Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^50) CHAPTER 5 ■ WHAT'S NEW IN PHP 6


Dynamic Static Methods


The __call() functionality allows you to create a wildcard type method that will handle all
undefined calls to methods to the class. The parameters of the method are the name of the
called method and an array of the parameters passed to the method. You can now create dynamic
static methods in the same manner as __call is used for non-static methods. With PHP 6, static
functionality is handled by implementing the magic method __callStatic() as shown in
Listing 5-14.

Listing 5-14. Using __callStatic for Dynamic Static Methods

<?php

class MyClass {
public static function __callStatic($name, $parameters) {
echo $name .' method called. Parameters: '. PHP_EOL.
var_export($parameters, true). PHP_EOL;
}
}

MyClass::bogus(1, false, 'a');

bogus method called. Parameters:
array (
0 => 1,
1 => false,
2 => 'a',
)

Ternary Assignment Shorthand (ifsetor)


One of the more common operations when working with input data is the use of the ternary
operator to provide a default value when an input is not present or fails to validate. You have
likely seen code like this:

$safe = $input? $input : 'default';

This is wordier than needed, so the middle value has been made optional. You can now
simply use the ifsetor syntax, ?:, like this:

$safe = $input ?: 'default';

XMLWriter Class


Creating XML documents is a key part of modern web programming. You will often need to
export data using XML formats. You have several existing options for creating XML documents,

McArthur_819-9C05.fm Page 50 Wednesday, February 27, 2008 8:38 AM

Free download pdf