📜 ⬆️ ⬇️

Innovations in PHP 5.6 beta 3



Since PHP is a developing language, I’ll talk about the features already implemented in the third beta release of version 5.6. In essence, this publication is an addition to the previous one: " Functions in PHP 5.6 - what's new? ".

The magic method is __debugInfo () .


Returns an array containing any information added by the developer. Called when an object is passed as an argument, for example, to the var_dump() function.

Example:
')
 namespace Application; class Test { public $prop = 200; public function __debugInfo() { return [1]; } } var_dump(new Test); 

Result of performance:

 object(Application\Test)#2 (1) { [0]=> int(1) } 

Note:

If you put the $this variable in the returned array, you will enter an infinite loop. :)

Exponential operator ** .


Here, I am sure, everything is clear and without a description. A simple operator in the form of two asterisks exponentially increasing the value of a number.

Example:

 $exponent = 2 ** 4; var_dump($exponent); // int(16) $exponent **= 2; var_dump($exponent); // int(256) unset($exponent); $exponent = ~2; var_dump($exponent); // int(-3) $exponent **= 2; var_dump($exponent); // int(9) 

Note:

It also works with GMP.

Example:

 $base = gmp_init(2); var_dump($base ** 3); 

Result of performance:

 object(GMP)#3 (1) { ["num"]=> string(1) "8" } 

Language constructs use function and use const .


Importing functions and constants (from namespaces) is like importing classes — cool. At first sight. When it comes to business, it works, at least for now, poorly.

Example:

 namespace Application { const TEST = 2 * 4; function test() { return TEST; } } namespace { use const Application\TEST; use function Application\test; var_dump(test(), TEST); } 

Result of performance:

 int(8) int(8) 

It seems to be good, but there is no autoload - it is implemented only for classes. So this "improvement", now, only adds the need to use additional syntax. From now on, if the namespace is specified, then the function cannot be accessed even by executing the include (or require ) file in which it is contained and you will have to add use function Name\Space\the_func . And if a lot of functions ..?

Improved autoload classes.


This, however, was fixed in version 5.5, but worth mentioning.

Example:

 spl_autoload_extensions('.php'); spl_autoload_register(); 

Previously, the above code would only work on a computer running Windows operating systems. But why? It's simple. The nesting separator for namespaces is the " \ " character, and in win-systems it is also a directory separator and it differs from that in unix systems. Because of this, something like this had to be done before:

 spl_autoload_register(function ($class_path) { require_once str_replace('\\', '/', $class_path) . '.php'; }); 

The default encoding is specified.


From now on, input_encoding , output_encoding and internal_encoding , by default, " UTF-8 ".

All implemented features can be found by clicking on the link: wiki.php.net/rfc#php_56

Source: https://habr.com/ru/post/233571/


All Articles