2. PERFORMANCE
• Over 140 bug fixes
• 40%+ improvement with PHP on Windows
• 5% - 15% overall performance improvement
• MD5 roughly 15% faster
• Constants move to read-only memory
• Drupal 20% faster, Wordpress 15% faster
3. ADDITIONS
• New error_reporting E_DEPRECATED
• Garbage collection
• MySQLnd (Native Driver)
• No longer uses libmysql
• No PDO support (currently)
• MySQL version 4.1+
4. BACKWARDS COMPATIBILITY
• EREG Family is now E_DEPRECATED
• Use the Pearl Compatible (PCRE)
• __toString does not accept arguments/parameters
• Magic methods must be public and can not be static
• __call is now invoked on access to private/protected methods
• Classes can not be named Namespace or Closure
SOURCES: http://us2.php.net/manual/en/migration53.incompatible.php
5. MAGIC METHODS IN 5.3
<?php
class Backwards {
public function __call($method, $value) {
echo "Call Magic Method<br />n";
}
private function __get($method) {
}
private function __set($method, $value) {
}
private function getElements() {
echo "Get Elements<br />n";
}
}
$bc = new Backwards();
$bc->getElements();
6. CHANGES IN PHP.INI
• INI Variables
• Per Folder/Per Site ini settings
• User specified ini files
7. .INI VARIABLES
error_dev = E_ALL
error_prod = E_NONE
[HOST=dev.mydomain.com]
error_reporting = ${error_dev}
[HOST=mydomain.com]
error_reporting = ${error_prod}
[PATH=/var/www/vhosts/myotherdomain.com]
error_reporting = ${error_prod}
# User Defined ini. Place in web root. Set to blank to disable
user_ini.filename = .user.ini
user_ini.cache_ttl = 300
8. SLOW ADOPTION
• Open Source projects were initially not compatible with PHP
5.3
• Currentlymost major Open Source software (Wordpress,
Drupal, Joomla and Magento) work in PHP 5.3
• Key plugins are lacking behind
15. GOTO EXAMPLE
<?php
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto end;
}
}
echo "i = $i";
end: // End
echo 'j hit 17';
SOURCES: http://php.net/manual/en/control-structures.goto.php
16. NOWDOC VS. HEREDOC
• NOWDOC works just like HEREDOC, except it does not
evaluate PHP variables
<?php
$myVar = 'testing';
// OUTPUT: Here is my text testing
$longString = <<<HEREDOC
Here is my text $myVar
HEREDOC;
// OUTPUT: Here is my text $myVar
$longString = <<<'NOWDOC'
Here is my text $myVar
'NOWDOC';
17. DATE/TIME OBJECT
ADDITIONS
• New functions/methods added to Date/Time Object
• date_add, date_sub and date_diff
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "n";
// OR
$date = date_create('200-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
SOURCES: http://www.php.net/manual/en/class.datetime.php
19. __INVOKE
<?php
class Functor {
public function __invoke($param = null)
{
return 'Hello Param: ' . $param;
}
}
$func = new Functor();
echo $func('PHP 5.3'); // Hello Param: PHP 5.3
20. DYNAMIC STATIC METHOD
<?php
abstract class Model
{
const TABLE_NAME = '';
public static function __call($method, $params)
{
// Run logic
return $this->$method($criteria, $order, $limit, $offset);
}
}
21. DYNAMIC STATIC METHOD
<?php
abstract class Model
{
const TABLE_NAME = '';
public static function __callStatic($method, $params)
{
// Run logic
return static::$method($criteria, $order, $limit, $offset);
}
}
22. __CALLSTATIC
• __callStatic works exactly like __call, except it’s a static
method
• Acts as a catch all for all undefined methods (get or set)
<?php
abstract class Model
{
const TABLE_NAME = '';
public static function __callStatic($method, $params)
{
// Run logic
return static::$method($criteria, $order, $limit, $offset);
}
}
24. GET_CLASS_CALLED
<?php
namespace App {
abstract class Model {
public static function __callStatic($method, $params)
{
// Search Logic
$method = $matches[1];
return static::$method($criteria, $order, $limit, $offset);
}
public static function find($criteria = array(), $order = null, $limit = null, $offset = 0)
{
$tableName = strtolower(get_class_called()); // get_class_called will return Post
}
}
}
namespace AppModels {
class Post extends AppModel {}
}
// Returns all the posts that contain Dallas PHP in the title
$posts = Post::findByTitle('Dallas PHP');
?>
25. LATE STATIC BINDING
• This feature was named "late static bindings" with an internal
perspective in mind. "Late binding" comes from the fact that
static:: will no longer be resolved using the class where the
method is defined but it will rather be computed using
runtime information.
SOURCES: http://us.php.net/lsb
26. LSB BEFORE PHP 5.3
<?php
class Model {
const TABLE_NAME = '';
public static function getTable()
{
return self::TABLE_NAME;
}
}
class Author extends Model {
const TABLE_NAME = 'Author';
}
class Post extends Model {
const TABLE_NAME = 'Post'
}
// sadly you get nothing
echo Post::TABLE_NAME;
27. LSB PHP 5.3.X
• static keyword to save the day!
<?php
class Model {
const TABLE_NAME = '';
public static function getTable()
{
return static::TABLE_NAME;
}
}
class Author extends Model {
const TABLE_NAME = 'Author';
}
class Post extends Model {
const TABLE_NAME = 'Post'
}
// sadly you get nothing
echo Post::TABLE_NAME;
28. LAMBDA IN PHP 5.3
• Anonymous functions, also known as closures, allow the
creation of functions which have no specified name. They are
most useful as the value of callback parameters, but they have
many other uses.
• Good function examples: array_map() and array_walk()
<?php
$string = 'Testing';
$array = array('hello', 'world', 'trying', 'PHP 5.3');
$return = array_walk($array, function($v,$k) {
echo ucwords($v);
});
31. NAMESPACES
• Before PHP 5.3
• PHP didn’t have namespaces, so we created a standard:
“Zend_Auth_Adapter_DbTable”
• PEAR naming convention
• Easier for autoloaders
32. DEFINING A NAMESPACE
<?php
namespace MyAppUtil;
class String
{
public function formatPhone($phone) {
// Regex phone number
return true;
}
}
33. “USE” A NAMESPACE
<?php
use MyAppUtilString as String;
$str = new String();
if ($str->formatPhone('123-456-7890')) {
echo 'ITS TRUE';
}
34. DEFINING MULTIPLE
NAMESPACES
One Way Preferred Way!
<?php <?php
use FrameworkController as BaseController use FrameworkController as BaseController
use FrameworkModel as BaseModel use FrameworkModel as BaseModel
namespace MyAppControllers; namespace MyAppControllers {
class UsersController extends BaseController class UsersController extends
{ BaseController
{
}
}
namespace MyAppUserModel; }
class User extends BaseModel
{ namespace MyAppUserModel {
class User extends BaseModel
} {
}
}
35. WHAT IS GLOBAL SCOPE?
• Global Scope is your “root level” outside of the namespace
<?php
namespace Framework;
class DB
{
public function getConnection()
{
try {
$dbh = new PDO('mysql:dbname=testdb;host=localhost', 'root', 'pass');
} catch (Exception $e) {
echo 'Default Exception';
}
}
}
$db = new DB();
$db = $db->getConnection();