Migrating to PHP 5.3 Stefan Priebsch July 31st 2009
Copyright © 2009 thePHP.cc, Germany
Premium PHP Consulting & Training. Worldwide.
Sebastian Bergmann
Arne Blankerts
Stefan Priebsch
Why PHP 5.3?
Faster
Better tested
Officially supported on all Windows platforms
Loads of new features
Selected PHP 5.3 Highlights
Namespaces
Late Static Binding
New extensions: fileinfo, intl, Phar
New datastructures in SPL
SQLite3
mysqlnd
Garbage Collection
Namespaces
Work for classes, functions and constants
Namespace separator: \
Work like directory navigation
Relative Path namespace foo\bar + class baz = foo\bar\baz namespace 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(...)
Namespaces Example namespace my\Application\Models; use Framework\Model\ModelBase as Model; class UserModel extends Model { ... }
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"
Late Static Binding Example class SubClass extends BaseClass { static protected $foo = 'bar'; } var_dump(SubClass::getFoo()); Will output: string(3) "foo"
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"
Late Static Binding Example class SomeClass { static public function foo() { var_dump('foo method'); } } $classname = 'SomeClass'; $classname::foo(); Will output: string(10) "foo method"
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!
Phar Example src/: ... any files ... stub.php: Phar::mapPhar('alias.phar'); ... __HALT_COMPILER(); create_phar.php: $phar = new Phar('executable.phar'); $phar->buildFromDirectory('./src'); $phar->setStub( file_get_contents('./stub.php'));
Migration Problems
Environment
Compile-time errors
Name Conflicts
More strictness
Runtime errors
Old Extensions
New error messages (E_DEPRECATED)
Logic errors
Altered behaviour
Altered function behaviour $haystack = 'hello world'; $needle = 'lo'; var_dump(strrpos($haystack, $needle));
Altered function behaviour $haystack = 'hello world'; $needle = 'lo'; var_dump(strrpos($haystack, $needle)); PHP 4: int(9)
Altered function behaviour $haystack = 'hello world'; $needle = 'lo'; var_dump(strrpos($haystack, $needle)); PHP 4: int(9) PHP 5: int(3)
Solution: Create a wrapper function strrpos_wrapper($haystack, $needle, $offset = 0) { return strrpos($haystack, substr($needle, 0, 1), $offset); }
More Strictness abstract class Test { abstract static function foo(); } Strict standards: Static function should not be abstract
More Strictness abstract class Test { abstract private function foo(); } Fatal error: Abstract function cannot be declared private
Useful Tools
Lint: php -l
Validators (HTML, XML, CSS, JS)
PHPUnit
Selenium
http://www.phpunit.de http://seleniumhq.org
xdebug
http://www.xdebug.org
xdebug Function Trace
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); }
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++]; }
Mocking include_once './mock_library/function.php'; for($i = 0; $i < 5; $i++) { var_dump(get_random_value()); }
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
FutureProof PHP Configuration
ze1.compatibility_mode=Off
allow_call_time_pass_reference=Off
register_globals=Off
magic_quotes_*=Off
register_long_arrays=Off
http://www.phparch.com/books/index
Code Works '09 Workshops
”Advanced OOP and Design Patterns” ”PHP Code Review” with Arne Blankerts and Sebastian Bergmann
http://cw.mtacon.com
Thank You. ■
http://thePHP.cc
■
http://www.priebsch.de
■
http://www.slideshare.net/spriebsch
■
http://twitter.com/spriebsch
■
[email protected], IRC: spriebsch
Copyright © 2009 thePHP.cc, Germany