PHP 5.4
New Language Features You Will Find
             Useful

          Sponsored by:
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…
Session_status()
• Returns current session status
• 2 if session is active
• Else 1
Some number stuff
• Binary notation
       $bin = 0b0111010;
       print_r($b);
• number_format now allows multibyte thousands seperator
  and decimal char - it used to truncate to first char for both
       echo number_format(123456789.654321, 6, '߀', '©Ü');
       => 123©Ü456©Ü789߀654321
• hex2bin() now provided (bin2hex already existed)
       $hex = bin2hex(“iauerv");
       $bin = hex2bin($hex);
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();
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();
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.
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”);
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];
JSON
• Some new JSON formats for json_encode()
     •   JSON_UNESCAPED_UNICODE
     •   JSON_BIGINT_AS_STRING
     •   JSON_NUMERIC_CHECK
     •   JSON_PRETTY_PRINT
     •    JSON_UNESCAPED_SLASHES
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()
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.
$closure =function($final_slide){printf($final_slide)}

Sponsor (venue & pizza)
      Tipping Canoe
Speaker
      David Engel
      davidengel.dev@gmail.com
Binaries & Tars
      http://qa.php.net/
      http://windows.php.net/qa/
Next RC should be out this week.

Php 5.4: New Language Features You Will Find Useful

  • 1.
    PHP 5.4 New LanguageFeatures You Will Find Useful Sponsored by:
  • 2.
    PHP 5.4 • UTF8multibyte 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…
  • 3.
    Session_status() • Returns currentsession status • 2 if session is active • Else 1
  • 4.
    Some number stuff •Binary notation $bin = 0b0111010; print_r($b); • number_format now allows multibyte thousands seperator and decimal char - it used to truncate to first char for both echo number_format(123456789.654321, 6, '߀', '©Ü'); => 123©Ü456©Ü789߀654321 • hex2bin() now provided (bin2hex already existed) $hex = bin2hex(“iauerv"); $bin = hex2bin($hex);
  • 5.
    Class invocation withoutconstruction • 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 callsby 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 • (newClassConstruct1)->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 callusing 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 offunctions • 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 newJSON formats for json_encode() • JSON_UNESCAPED_UNICODE • JSON_BIGINT_AS_STRING • JSON_NUMERIC_CHECK • JSON_PRETTY_PRINT • JSON_UNESCAPED_SLASHES
  • 11.
    Traits • Provides functionalityto 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 resultstraceble • 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.
  • 13.
    $closure =function($final_slide){printf($final_slide)} Sponsor (venue& pizza) Tipping Canoe Speaker David Engel davidengel.dev@gmail.com Binaries & Tars http://qa.php.net/ http://windows.php.net/qa/ Next RC should be out this week.