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

Learning Swift2 with PHP7

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☻

Norio Suzuki

November 24, 2015
Tweet

More Decks by Norio Suzuki

Other Decks in Programming

Transcript

  1. Learning Swift2 with PHP7
    PHP BLT #1
    2015-11-24
    @suzuki

    View Slide

  2. Lightning Talks Driven Learning
    This talk for my study.

    Perhaps it is not interesting for you☻

    View Slide

  3. Language Specifications
    PHP7 : Swift2

    View Slide

  4. 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

    View Slide

  5. comment
    /*
    * 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. */
    */

    View Slide

  6. type
    $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 = ["a":1, "b":2]
    // "!" is cast for Optional tyep
    print(dic["a"]!)

    View Slide

  7. access
    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 {}

    View Slide

  8. operator
    $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)

    View Slide

  9. if
    $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 {
    }

    View Slide

  10. switch
    $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"
    }

    View Slide

  11. for
    $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);
    }

    View Slide

  12. while
    $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

    View Slide

  13. function
    // 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 {
    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)
    }

    View Slide

  14. class
    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())

    View Slide

  15. Swift Application

    View Slide

  16. CLI with Swift2
    http://techlife.cookpad.com/entry/2015/11/09/150248

    View Slide

  17. CLI with Swift2
    http://techlife.cookpad.com/entry/2015/11/09/150248

    View Slide

  18. #!/usr/bin/env swift

    View Slide

  19. So…

    View Slide

  20. View Slide

  21. Web Application
    PHP7 : Swift2

    View Slide

  22. system structure
    nginx
    php-fpm
    php7
    index.php
    nginx
    fcgiwrap
    swift2
    index.swift

    View Slide

  23. form

    View Slide

  24. confirm

    View Slide

  25. complete

    View Slide

  26. execute
    $view = new View();
    $app = new App($view);
    $app->run();
    #!/usr/bin/env swift
    let view = View();
    let app = App(view: view);
    app.run()

    View Slide

  27. 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);
    }

    View Slide

  28. 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 {
    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 = [
    "\"": """,
    "'": "'",
    "<": "<",
    ">": ">",
    ]
    for (key, value) in replaceDef {
    let pattern = key;
    let replace = value;
    html = html.stringByReplacingOccurrencesOfString(pattern, withString:
    replace,
    options: NSStringCompareOptions.RegularExpressionSearch, range: nil)
    }
    return html
    }

    View Slide

  29. other code
    https://github.com/suzuki/learning-swift2-with-php7

    View Slide

  30. Conclusion

    View Slide

  31. • Swift2 can use in web applications, maybe.

    View Slide

  32. perfect
    http://perfect.org/

    View Slide

  33. • Let's use the new syntax of PHP7.
    • It would be helpful to learn other programing languages.

    View Slide

  34. Thank you

    View Slide

  35. 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

    View Slide