Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Learning Swift2 with PHP7

Avatar for Norio Suzuki Norio Suzuki
November 24, 2015

Learning Swift2 with PHP7

Lightning Talks Driven Learning.

This talk for my study.

Perhaps it is not interesting for you☻

Avatar for Norio Suzuki

Norio Suzuki

November 24, 2015
Tweet

More Decks by Norio Suzuki

Other Decks in Programming

Transcript

  1. language version % php --version PHP 7.0.0RC6 (cli) (built: Oct

    29 2015 13:49:53) ( NTS ) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies % swift --version Apple Swift version 2.1 (swiftlang-700.1.101.6 clang-700.1.76) Target: x86_64-apple-darwin14.5.0
  2. comment <?php /* * This is a comment. */ //

    This is also a comment. # This is comment, too. // /* // * /* Nested comment not work. */ // */ /* * This is a comment. */ // This is also a comment. // "#" is not a comment syntax. /* * /* Nested comment works. */ */
  3. type <?php $intVar = 123; $floatVar = 1.23; $doubleQoute =

    "string"; $singleQuote = 'string'; $arrayVar = [1, 2, 3]; $hashVar = ['a' => 1, 'b' => 2]; echo $hashVar['a']; var intVar1 = 123 var intVar2:Int = 123 var floatVar1 = 1.23 var floatVar2:Float = 1.23 var doubleQuote1 = "string" var doubleQuote2:String = "string" // single quote is not work //var singleQuote1 = 'string' //var singleQuote2:String = 'string' var arrayVar = [1, 2, 3] var dic:Dictionary<String, Int> = ["a":1, "b":2] // "!" is cast for Optional tyep print(dic["a"]!)
  4. access <?php define('GLOBAL_CONST', 'global const'); $global = 'global var'; class

    ExampleClass { private $privateVar = 'private var'; protected $protectedVar = 'protected var'; public $publicVar = 'public var'; const CLASS_CONST = 'class const'; } // Behavior is different from PHP // These comparison is not correct. let GLOBAL_CONST = "global const" var global = "global var" public class ExampleClass { private var privateVar = "private var" internal var internalVar = "internal var" public var publicVar = "public var" private let privateConst = "private const" internal let internalConst = "inetrnal const" public let publicConst = "public const" } private class PrivateClass {} internal class InternalClass {} public class PublicClass {}
  5. operator <?php $intA = 1; $intB = 2; $intC =

    $intA + $intB; echo $intC; $strA = "a"; $strB = "b"; $strC = $strA . $strB; echo $strC; $nullVar = null; $notNullVar = 'not null'; $result = $nullVar ?? $notNullVar; // PHP7 echo $result; var intA = 1 var intB = 2 var intC = intA + intB print(intC) var strA = "a" var strB = "b" var strC = strA + strB print(strC) var nilVar:String? = nil var notNilVar = "not nil" var result = nilVar ?? notNilVar print(result)
  6. if <?php $a = 123; $b = 456; if ($a

    > $b) { } elseif ($a < $b) { } else { } var a = 123 var b = 456 // Parens can be ommitted. if a > b { } else if a < b { } else { }
  7. switch <?php $foo = 1; $bar = ''; switch ($foo)

    { case 1: $bar = 'a'; break; case 2: $bar = 'b'; break; default: $bar = 'c'; } var foo = 1 var bar = "" switch foo { case 1: bar = "a" // no need "break" case 2: bar = "b" default: bar = "c" }
  8. for <?php $array = []; for ($i = 0; $i

    < 10; $i++) { $array[] = $i; } foreach ($array as $tmp) { echo $tmp; } var array:[Int] = []; for i in 0..<10 { array.append(i) } for tmp in array { print(tmp); }
  9. while <?php $max = 3; $counter1 = 0; $counter2 =

    0; while ($counter1 < $max) { echo 'Hi!'; $counter1++; } do { echo 'Ho!'; $counter2++; } while ($counter2 < $max); let max = 3; var counter1 = 0; var counter2 = 0; while counter1 < max { print("Hi!") counter1++ } repeat { print("ho!") counter2++ } while counter2 < max
  10. function <?php // PHP7: scalar type hints, return type hints

    function foo(string $str): array { $result = []; for ($i = 0; $i < 10; $i++) { $result[] = $str . $i; } return $result; } $list = foo('x'); foreach ($list as $l) { echo $l; } 
 func foo(str: String) -> Array<String> { var result:[String] = [] for i in 0..<10 { result.append(str + String(i)) } return result } let list = foo("x") for l in list { print(l) }
  11. class <?php interface ExampleInterface { public function foo(): string; }

    class Base { protected $bar = 'bar'; } class Example extends Base implements ExampleInterface { public function __construct() { echo 'initialize'; } public function foo(): string { return $this->bar; } } $example = new Example(); echo $example->foo(); protocol ExampleProtocol { func foo() -> String } class Base { internal let bar = "bar" } class Example: Base, ExampleProtocol { override init() { print("initialize") } func foo() -> String { return self.bar } } let example = Example() // no need 'new' print(example.foo())
  12. execute <?php $view = new View(); $app = new App($view);

    $app->run(); #!/usr/bin/env swift let view = View(); let app = App(view: view); app.run()
  13. App::run() public function run() { $params = $this->parseGetVars(); $html =

    ''; $next = $params['next'] ?? ''; switch ($next) { case 'confirm': $html = $this->view->getConfirmHtml($params); break; case 'complete': $html = $this->view->getCompleteHtml($params); break; default: $html = $this->view->getFormHtml($params); } $this->response($html); } func run() { let params = self.parseGetVars() var html = "" let next = params["next"] ?? "" switch next { case "confirm": html = self.view.getConfirmHtml(params) case "complete": html = self.view.getCompleteHtml(params) default: html = self.view.getFormHtml(params) } self.response(html); }
  14. htmlspecialchars private function renderTemplate( string $html, array $params ): string

    { $replaceKey = []; $replaceValue = []; foreach ($params as $key => $value) { $replaceKey[] = '/{{ ' . $key . ' }}/'; $replaceValue[] = htmlspecialchars( $value, ENT_QUOTES , 'UTF-8' ); } $replaceKey[] = '/{{ action }}/'; $replaceValue[] = 'index.php'; return preg_replace( $replaceKey, $replaceValue, $html); } private func renderTemplate(html: String, params: Dictionary<String, String>) -> String { let pattern = "\\{\\{ action \\}\\}" let replace = "index.swift" var html = html.stringByReplacingOccurrencesOfString(pattern, withString: replace, options: NSStringCompareOptions.RegularExpressionSearch, range: nil) for (key, value) in params { let pattern = "\\{\\{ " + key + " \\}\\}" let replace = self.htmlspecialchars(value) html = html.stringByReplacingOccurrencesOfString(pattern, withString: replace, options: NSStringCompareOptions.RegularExpressionSearch, range: nil) } return html; } private func htmlspecialchars(var html: String) -> String { let replaceDef: Dictionary<String, String> = [ "\"": "&quot;", "'": "&#039;", "<": "&lt;", ">": "&gt;", ] for (key, value) in replaceDef { let pattern = key; let replace = value; html = html.stringByReplacingOccurrencesOfString(pattern, withString: replace, options: NSStringCompareOptions.RegularExpressionSearch, range: nil) } return html }
  15. • Let's use the new syntax of PHP7. • It

    would be helpful to learn other programing languages.
  16. Reference • Swift2Ͱ࡞ΔίϚϯυϥΠϯπʔϧ • http://techlife.cookpad.com/entry/2015/11/09/150248 • Swift 2ඪ४ΨΠυϒοΫ • http://tatsu-zine.com/books/swift2-hyojyun-guide

    • PHP7ͰมΘΔ͜ͱ ——ݴޠ࢓༷ͱΤϯδϯͷվળϙΠϯτ • http://www.slideshare.net/hnw/phpcon-kansai20150530 • Apple͕SwiftΛΦʔϓϯιʔεԽ • http://www.infoq.com/jp/news/2015/06/swift-opensourced