Category Archives: PHP

Late Static Bindings (PHP 5.3)

// Limitation of Self:: in PHP 5.2 
// before Late Static Binding PHP 5.3
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        self::who();
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test(); // This will output: A

// Late Static Binding in PHP 5.3
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        static::who(); // Late Static Bindings
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test(); // This will output: B

php 5.5 new and deprecated features

Since php 5.5 is out now, There are some cool features and deprecated features I like to mentioned here.

PHP 5.5 New features

“Generators” support added
Support for generators has been added via the yield keyword. Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface.

“Finally” keyword support added
try-catch blocks now support a finally block for code that should be run regardless of whether an exception has been thrown or not.

foreach now supports list()
The foreach control structure now supports unpacking nested arrays into separate variables via the list() construct.

empty() supports arbitrary expressions
Passing an arbitrary expression instead of a variable to empty() is now supported.

array and string literal dereferencing
Array and string literals can now be dereferenced directly to access individual elements and characters:

New password hashing API
A new password hashing API that makes it easier to securely hash and manage passwords using the same underlying library as crypt() in PHP has been added.

Apache 2.4 handler supported on Windows
The Apache 2.4 handler SAPI is now supported on Windows.

Improvements to GD
Various improvements have been made to the GD extension

php 5.5 deprecated features

ext/mysql deprecation
The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions.

preg_replace() /e modifier
The preg_replace() /e modifier is now deprecated. Instead, use the preg_replace_callback() function.

intl deprecations
IntlDateFormatter::setTimeZoneID() and datefmt_set_timezone_id() are now deprecated. Instead, use the IntlDateFormatter::setTimeZone() method and datefmt_set_timezone() function, respectively.

mcrypt deprecations
The following functions have been deprecated:
mcrypt_cbc()
mcrypt_cfb()
mcrypt_ecb()
mcrypt_ofb()

Ref1. http://www.php.net/manual/en/migration55.new-features.php
Ref2. http://www.php.net/manual/en/migration55.deprecated.php

PHP 5.4

The key features of PHP 5.4.0 include:

  • New language syntax including Traits, shortened array syntax and more
  • Improved performance and reduced memory consumption
  • Support for multibyte languages now available in all builds of PHP at the flip of a runtime switch
  • Built-in webserver in CLI mode to simplify development workflows and testing
  • Cleaner code base thanks to the removal of multiple deprecated language features
  • Many more improvements and fixes

Changes that affect compatibility:

Extensions moved to PECL:

For users upgrading from PHP 5.3 there is a migration guide available here, detailing the changes between PHP 5.3 and PHP 5.4.0.

PHP 5.3

The key features of PHP 5.3.0 include:

This release also drops several extensions and unifies the usage of internal APIs. Users should be aware of the following known backwards compatibility breaks:

For users upgrading from PHP 5.2 there is a migration guide available here, detailing the changes between those releases and PHP 5.3.0.