Migrating to PHP 5.3

download Migrating to PHP 5.3

of 29

Transcript of Migrating to PHP 5.3

  • 8/14/2019 Migrating to PHP 5.3

    1/29

    Copyright 2009 thePHP.cc, Germany

    Migrating to PHP 5.3

    Stefan Priebsch

    July 31st 2009

  • 8/14/2019 Migrating to PHP 5.3

    2/29

    Premium PHP Consulting & Training. Worldwide.

    ArneBlankerts

    SebastianBergmann

    StefanPriebsch

  • 8/14/2019 Migrating to PHP 5.3

    3/29

    Why PHP 5.3?

    Faster Better tested

    Officially supported on all Windows platforms

    Loads of new features

  • 8/14/2019 Migrating to PHP 5.3

    4/29

    Selected PHP 5.3 Highlights

    Namespaces Late Static Binding

    New extensions: fileinfo, intl, Phar

    New datastructures in SPL SQLite3

    mysqlnd

    Garbage Collection

  • 8/14/2019 Migrating to PHP 5.3

    5/29

    Namespaces

    Work for classes, functions and constants Namespace separator: \

    Work like directory navigation

    Relative Pathnamespace foo\bar + class baz = foo\bar\baznamespace foo + class bar\baz = foo\bar\baz

    Absolute Path

    namespace foo\bar + class \baz = \baz Prepend built-in classes and functions with \

    throw new \Exception

    \strlen(...)

  • 8/14/2019 Migrating to PHP 5.3

    6/29

    Namespaces Example

    namespace my\Application\Models;use Framework\Model\ModelBase as Model;

    class UserModel extends Model

    {...

    }

  • 8/14/2019 Migrating to PHP 5.3

    7/29

    Late Static Binding Example

    class BaseClass{static protected $foo = 'foo';

    static public function getFoo()

    {return self::$foo;

    }}

    var_dump(BaseClass::getFoo());

    Will output: string(3) "foo"

  • 8/14/2019 Migrating to PHP 5.3

    8/29

    Late Static Binding Example

    class SubClass extends BaseClass{static protected $foo = 'bar';

    }

    var_dump(SubClass::getFoo());

    Will output: string(3) "foo"

  • 8/14/2019 Migrating to PHP 5.3

    9/29

    Late Static Binding Example

    class BaseClass{static protected $foo = 'foo';

    static public function getFoo()

    {return static::$foo;

    }}

    var_dump(SubClass::getFoo());

    Will output: string(3) "bar"

  • 8/14/2019 Migrating to PHP 5.3

    10/29

    Late Static Binding Example

    class SomeClass{static public function foo(){

    var_dump('foo method');

    }}

    $classname = 'SomeClass';

    $classname::foo();

    Will output: string(10) "foo method"

  • 8/14/2019 Migrating to PHP 5.3

    11/29

    Phar

    PHP Archive Stream wrapper-based

    PHP extension + Userland implementation

    Can be executable No temporary files involved

    php.ini: phar.readonly=Off;

    You cannot set this at runtime!

  • 8/14/2019 Migrating to PHP 5.3

    12/29

  • 8/14/2019 Migrating to PHP 5.3

    13/29

    Migration Problems

    Environment Old Extensions

    Compile-time errors

    Name Conflicts More strictness

    Runtime errors

    New error messages (E_DEPRECATED) Logic errors

    Altered behaviour

  • 8/14/2019 Migrating to PHP 5.3

    14/29

    Altered function behaviour

    $haystack = 'hello world';$needle = 'lo';

    var_dump(strrpos($haystack, $needle));

  • 8/14/2019 Migrating to PHP 5.3

    15/29

    Altered function behaviour

    $haystack = 'hello world';$needle = 'lo';

    var_dump(strrpos($haystack, $needle));

    PHP 4: int(9)

  • 8/14/2019 Migrating to PHP 5.3

    16/29

    Altered function behaviour

    $haystack = 'hello world';$needle = 'lo';

    var_dump(strrpos($haystack, $needle));

    PHP 4: int(9)

    PHP 5: int(3)

  • 8/14/2019 Migrating to PHP 5.3

    17/29

    Solution: Create a wrapper

    function strrpos_wrapper($haystack,$needle,$offset = 0)

    {return strrpos($haystack,

    substr($needle, 0, 1),$offset);

    }

  • 8/14/2019 Migrating to PHP 5.3

    18/29

    More Strictness

    abstract class Test{ abstractstatic function foo();}

    Strict standards:Static function should not be abstract

  • 8/14/2019 Migrating to PHP 5.3

    19/29

    More Strictness

    abstract class Test{ abstractprivate function foo();}

    Fatal error:Abstract function cannot be declared private

  • 8/14/2019 Migrating to PHP 5.3

    20/29

    Useful Tools

    Lint: php -l Validators (HTML, XML, CSS, JS)

    PHPUnit

    http://www.phpunit.de Selenium

    http://seleniumhq.org

    xdebug http://www.xdebug.org

  • 8/14/2019 Migrating to PHP 5.3

    21/29

    xdebug Function Trace

  • 8/14/2019 Migrating to PHP 5.3

    22/29

    Mocking

    include_once './library/function.php';

    for($i = 0; $i < 5; $i++) {var_dump(get_random_value());

    }

    library/function.php:

    function get_random_value(){

    return rand(1, 10);}

  • 8/14/2019 Migrating to PHP 5.3

    23/29

    Mocking

    mock_library/function.php:

    function get_random_value(){

    static $i = 0;static $random_values =array(1, 2, 3, 4, 5);

    return $random_values[$i++];

    }

    k

  • 8/14/2019 Migrating to PHP 5.3

    24/29

    Mocking

    include_once './mock_library/function.php';

    for($i = 0; $i < 5; $i++) {var_dump(get_random_value());

    }

  • 8/14/2019 Migrating to PHP 5.3

    25/29

    How To Migrate

    Fix errors/warnings/notices Normalize the PHP configuration

    Set up two test systems, old and new

    Do A/B testing Compare the results

    Fix the problems

    Switch the production system

    f fi i

  • 8/14/2019 Migrating to PHP 5.3

    26/29

    Future-Proof PHP Configuration

    ze1.compatibility_mode=Off allow_call_time_pass_reference=Off

    register_globals=Off

    magic_quotes_*=Off register_long_arrays=Off

  • 8/14/2019 Migrating to PHP 5.3

    27/29

    http://www.phparch.com/books/index

    C d W k '09 W k h

  • 8/14/2019 Migrating to PHP 5.3

    28/29

    Code Works '09 Workshops

    Advanced OOP and Design Patterns PHP Code Review with Arne Blankerts

    and Sebastian Bergmann

    http://cw.mtacon.com

  • 8/14/2019 Migrating to PHP 5.3

    29/29

    Copyright 2009 thePHP.cc, Germany

    Thank You. http://thePHP.cc

    http://www.priebsch.de http://www.slideshare.net/spriebsch

    http://twitter.com/spriebsch

    [email protected], IRC: spriebsch