PHP 5.4 includes several new features including improved UTF8 support, session status functions, binary syntax, number formatting improvements, and support for traits. It also includes enhancements to classes like allowing constructor-less instantiation and calling static methods via strings. Arrays have a new short syntax and functions can now be dereferenced.
2. PHP 5.4
• UTF8 multibyte support now enabled by default in install
• session_status(), Binary syntax, number_format(), hex2bin()
• Classes
– invocation without constructor
– Class::{expr}() support for static methods
– Class constructor chaining
– Indirect method call using array
• Arrays
– Short syntax
– Dereferencing of functions
• mysqli results traversable
• Traits
• Some JSON stuff and more…
5. Class invocation without construction
• ReflectionClass::newInstanceWithoutConstructor
• Creates a new class instance without invoking the constructor.
class ClassConstruct1{
public function __construct(){
echo "Class #1 constructor";
}
public function whatisit(){
echo 'It is Class #1';
}
}
$cc2 = new ReflectionClass("ClassConstruct1");
$func = $cc2->newInstanceWithoutConstructor();
$func->whatisit();
6. Class::{expr}()
• Static calls by string or variable previously unsupported
class ClassConstruct1{
public function __construct(){
echo "Class #1 constructor";
}
public static function 'staticwhatisit(){
echo 'It is Class #1';
}
}
$func = 'staticwhatisit';
$test = new ClassConstruct1();
echo "<br>";
$test::$func();
7. Constructor chaining
• (new ClassConstruct1)->whatisit();
• NOTE: the type hints and the number of
required arguments must be the same now for
constructors of abstract classes and their
extensions – previously NOT the case. In 5.3 a
different type hint would not fail. In 5.4 it will
throw a catchable error.
8. Indirect method call using array
• call a function from an array using class name
and function name as parameters
class ClassConstruct{
public function __construct(){
}
public static function dosomething($animal){
echo "I am a classy $animal.";
}
}
$func = array('ClassConstruct', 'dosomething');
$func(“cow”);
9. Array dereferencing of functions
• Dereference the returned array without assigning to a var!!!
class FooBar
{
protected $foobar_array = array();
public function __construct()
{
// NOTE THE JAVA-ESQUE SHORT SYNTAX BELOW
$this->foobar_array = [0 => 'foo',1 => 'bar'];
}
public function returnArray()
{
return $this->foobar_array;
}
}
$ac = new FooBar();
echo $ac->returnArray()[0] . $ac->returnArray()[1];
10. JSON
• Some new JSON formats for json_encode()
• JSON_UNESCAPED_UNICODE
• JSON_BIGINT_AS_STRING
• JSON_NUMERIC_CHECK
• JSON_PRETTY_PRINT
• JSON_UNESCAPED_SLASHES
11. Traits
• Provides functionality to be inherited but not
instantiated.
• Groups functionality that is related by
methodology, but not type.
• Implemented by the use keyword.
• Provides means for conflict resolution using
the insteadof keyword
• Child f() > trait f() > parent/abstract f()
12. Other…
• mysqli results traceble
• JsonSerializable::jsonSerialize mixed class
• SCANDIR_SORT_NONE added for scandir to skip sorting – faster?
Dunno.
• Some new CURL extensions (limit your server BW consumption)
CURLOPT_MAX_RECV_SPEED_LARGE
CURLOPT_MAX_SEND_SPEED_LARGE
• Some new Iterator functionality
– RegexIterator::getRegex() returns the regex
– CallbackFilterIterator can take an iterator (with a set of data) and apply a
filter function to it as it iterates.