MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

5 Incredible Features of PHP 5.4 Every Developer Should Use

Published on 09 December 15
391
0
1
5 Incredible Features of PHP 5.4 Every Developer Should Use - Image 1
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.




































































































5 Incredible Features of PHP 5.4 Every Developer Should Use - Image 1

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.

This blog is listed under Open Source and Development & Implementations Community

Related Posts:
Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
You may also be interested in
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url

Back to Top