5 Incredible Features of PHP 5.4 Every Developer Should Use
PHP is all set to release the PHP 7, this year end. PHP 6 did not change much for the developers. If you are still one of those developers who prefer developing on PHP 5 especially the PHP 5.4 version, then here are a few features that you should be using, especially if you are not using them.
Traits
You would have heard core PHP developers working with this feature. It is basically a compiler assisted copy-paste feature, Most of the other languages refer to Traits as Mixins. The traits include conflict resolution feature, in case you have multiple traits that have the same feature.
trait Singleton {
public static function getInstance() { ... }
}
class A {
use Singleton;
// ...
}
class B extends ArrayObject {
use Singleton;
// ...
}
// Singleton method is now available for both classes
A::getInstance();
B::getInstance();
Apart from conflict resolution, this feature also allows visibility, method precedence, support for constants and other functionality.
Instance Method Call
When you want to perform function array dereferencing, you can call a method using object instantiation. Here is the code that you can use for this purpose
class foo {
public $x = 1;
public function getX() {
return $this->x;
}
public function setX($val) {
$this->x = $val;
return $this;
}
}
$X = (new foo)->setX(20)->getX();
echo $X; // 20
Instead of the instantiated object, you can use a static method call. Combine it with short array syntax and function array dereferencing, and the following code will be generated
class foo extends ArrayObject {
public function __construct($arr) {
parent::__construct($arr);
}
}
echo (new foo( [1, [4, 5], 3] ))[1][0];
Closure Binding
In PHP 5.4, the way closure binding interacts has been refined to help develop your application
class Foo {
private $prop;
function __construct($prop) {
$this->prop = $prop;
}
public function getPrinter() {
return function() { echo ucfirst($this->prop); };
}
}
$a = new Foo('bar');;
$func = $a->getPrinter();
$func(); // Outputs: Bar
This closure will access the $this->prop which happens to be a private property. Normally, closures in PHP use early binding. This simply means that the variables defined when the closure was defined will be maintained till the end. You can re-bound the closures
$a = new Foo('bar');
$b = new Foo('pickle');
$func = $a->getPrinter();
$func(); // Outputs: Bar
$func = $func->bindTo($b);
$func(); // Outputs: Pickle
If you don’t want the closure to be accessed, you can mention it as static closure
class Foo {
private $prop;
function __construct($prop) {
$this->prop = $prop;
}
public function getPrinter() {
return static function() { echo ucfirst($this->prop); };
}
}
Short Array Syntax
If you use this syntax, you don’t need to use the array keyword when defining an array
$a = [1, 2, 3];
$b = ['foo' => 'orange', 'bar' => 'apple'];
Object as Functions
You can use “invoke” when defining objects as functions. Here is the syntax that you can use for this purpose
class MoneyObject {
private $value;
function __construct($val) {
$this->value = $val;
}
function __invoke() {
return sprintf('$%.2f',$this->value);
}
}
Conclusion
PHP 5.4 is widely used to develop applications because of the interesting features it offers. You can change from dynamic content to static content using the different features available with this open source platform. Hire PHP developers to develop websites with some of the best features available.
Traits
You would have heard core PHP developers working with this feature. It is basically a compiler assisted copy-paste feature, Most of the other languages refer to Traits as Mixins. The traits include conflict resolution feature, in case you have multiple traits that have the same feature.
trait Singleton {
public static function getInstance() { ... }
}
class A {
use Singleton;
// ...
}
class B extends ArrayObject {
use Singleton;
// ...
}
// Singleton method is now available for both classes
A::getInstance();
B::getInstance();
Apart from conflict resolution, this feature also allows visibility, method precedence, support for constants and other functionality.
Instance Method Call
When you want to perform function array dereferencing, you can call a method using object instantiation. Here is the code that you can use for this purpose
class foo {
public $x = 1;
public function getX() {
return $this->x;
}
public function setX($val) {
$this->x = $val;
return $this;
}
}
$X = (new foo)->setX(20)->getX();
echo $X; // 20
Instead of the instantiated object, you can use a static method call. Combine it with short array syntax and function array dereferencing, and the following code will be generated
class foo extends ArrayObject {
public function __construct($arr) {
parent::__construct($arr);
}
}
echo (new foo( [1, [4, 5], 3] ))[1][0];
Closure Binding
In PHP 5.4, the way closure binding interacts has been refined to help develop your application
class Foo {
private $prop;
function __construct($prop) {
$this->prop = $prop;
}
public function getPrinter() {
return function() { echo ucfirst($this->prop); };
}
}
$a = new Foo('bar');;
$func = $a->getPrinter();
$func(); // Outputs: Bar
This closure will access the $this->prop which happens to be a private property. Normally, closures in PHP use early binding. This simply means that the variables defined when the closure was defined will be maintained till the end. You can re-bound the closures
$a = new Foo('bar');
$b = new Foo('pickle');
$func = $a->getPrinter();
$func(); // Outputs: Bar
$func = $func->bindTo($b);
$func(); // Outputs: Pickle
If you don’t want the closure to be accessed, you can mention it as static closure
class Foo {
private $prop;
function __construct($prop) {
$this->prop = $prop;
}
public function getPrinter() {
return static function() { echo ucfirst($this->prop); };
}
}
Short Array Syntax
If you use this syntax, you don’t need to use the array keyword when defining an array
$a = [1, 2, 3];
$b = ['foo' => 'orange', 'bar' => 'apple'];
Object as Functions
You can use “invoke” when defining objects as functions. Here is the syntax that you can use for this purpose
class MoneyObject {
private $value;
function __construct($val) {
$this->value = $val;
}
function __invoke() {
return sprintf('$%.2f',$this->value);
}
}
Conclusion
PHP 5.4 is widely used to develop applications because of the interesting features it offers. You can change from dynamic content to static content using the different features available with this open source platform. Hire PHP developers to develop websites with some of the best features available.
Advertise on APSense
This advertising space is available.
Post Your Ad Here
Post Your Ad Here
Comments