PHP 5.4 new features
Master
traits
The introduction of traits can expand the content of the class, making the class realize multiple inheritance in some form, making it more flexible
Trait cannot be instantiated
Sample code:
- trait Hello {
- public function sayHello() {
- echo 'Hello ' . "n";
- }
- }
- trait World {
- public function sayWorld() {
- echo 'World' . "n";
- }
- }
- class MyHelloWorld {
- use Hello, World;
- public function sayExclamationMark() {
- echo '!' . "n";
- }
- }
- $o = new MyHelloWorld();
- $ o->sayHello();
- $o->sayWorld();
- $o->sayExclamationMark();
-
Copy code
It should be noted that the inheritance order of traits is:
Members from the current class override the trait's methods, and the trait overrides the inherited methods
When multiple traits are used by the same class, method conflicts will occur. Use the keyword insteadof to solve the problem
Sample code:
- trait A {
- public function smallTalk() {
- echo 'a';
- }
- public function bigTalk() {
- echo 'A';
- }
- }
- trait B {
- public function smallTalk() {
- echo 'b';
- }
- public function bigTalk() {
- echo 'B';
- }
- }
- class Talker {
- use A, B {
- B::smallTalk instead of A;
- A ::bigTalk instead of B;
- }
- }
- class Aliased_Talker {
- use A, B {
- B::smallTalk instead of A;
- A::bigTalk instead of B;
- B::bigTalk as talk;
- }
- }
Copy code
New short array syntax
- $a = [1, 2, 3, 4];
- $a = ['one' => 1, 'two' => 2, 'three' => 3 , 'four' => 4];
-
Copy code
Added support for member access analysis of arrays returned by functions
- function foo()
- {
- return array(1,3,4,5);
- }
- $var =foo()[0];
Copy code
Now regardless of whether the short_open_tag php.ini option is set or not, = will always be available= 将总是可用
Added access to class members during instantiation
- class Test
- {
- public function foo()
- {
- //todo
- return 1;
- }
- }
- $var = (new Test)->foo();
-
Copy code
ps: Pay attention to the brackets
SESSION extension can now track file upload progress
Configure session.upload_progress.enabled = On in php.ini,
The file upload progress tracking function will be turned on
max_input_vars directive
In the php.ini file, set the value of max_input_vars,
Control the maximum length of $_GET, $_POST and $_COOKIE
Reduce the possibility of constructing hash collisions for denial of service attacks
Understand
Now closures support $this
Now supports Class::{expr}() syntax
Sample code:
- class Utils
- {
- public static function test1()
- {
- echo 1;
- }
- public static function test2()
- {
- echo 2;
- }
- }
- $m = ' test';
- Utils::{$m . (10-8)}();
- Utils::test2();
Copy code New binary direct value, for example: 0b001001101
New features in PHP 5.5
Master
opcache is integrated in the php distribution package
opcache is actually the Zend Optimizer Plus of zend company, with similar functions to apc
It performs better than apc, opcode is optimized, more information about opcache
Please go to Bird Brother’s blog
A little sharing about Zend O+
Therefore, when installing PHP, the compilation parameters must be added
- --enabled-opcache
- Recommended configuration (php.ini)
- zend_extension=opcache.so
- opcache.enable_cli=1
- opcache.memory_consumption=128 //Shared memory size, this can be adjusted according to your needs
- opcache .interned_strings_buffer=8 //The memory size of interned string can also be adjusted
- opcache.max_accelerated_files=4000 //The maximum number of cached files
- opcache.revalidate_freq=60 //Check file updates every 60s
- opcache.fast_shutdown=1 //Open Quick shutdown, when opening this in PHP Request Shutdown, the speed of memory recovery will be increased
- opcache.save_comments=0 //Do not save comments of files/functions
Copy code
Generators
Students who are familiar with python will definitely be familiar with generators
Its function is to use the keyword yield in a function to interrupt function execution and return an iterator that can be used in foreach
This syntax should be the most exciting feature in PHP5.5
The following code demonstrates the usage of the generator and makes a comparison to highlight the advantages of the generator.
- ini_set('memory_limit', '512M');
- $cmd = isset($argv[1]) ? (int)$argv[1] : 0;
- function xrange($ start, $end, $step = 1)
- {
- if($start <= $end)
- {
- if($step <= 0)
- {
- throw new LogicException('step must be +ve') ;
- }
- for($i = $start; $i <= $end; $i = $i + $step)
- {
- yield $i;
- }
- }else{
- if($step >= 0)
- {
- throw new LogicException('step must be -ve');
- }
- for($i = $start; $i >= $end; $i = $i + $step)
- {
- yield $i;
- }
- }
- }
-
- if($cmd == 0)
- {
- $r = range(1, 1000000);
- foreach($r as $v)
- {
- if($v > 20)
- {
- break;
- }
- echo "$vt";
- }
- $m = memory_get_usage(true);
- echo "n" . $m/1014/1024 . "Mn";
- }else if($ cmd == 1){
- $r = xrange(1, 1000000);
- foreach($r as $v)
- {
- if($v > 20)
- {
- break;
- }
- echo "$vt" ;
- }
- $m = memory_get_usage(true);
- echo "n" . $m / 1014 /1024 . "Mn";
- }else{
- include 'php-excel.class.php';
- $stime = microtime (true);
- if($cmd == 3)
- {
- $data = array();
- for($i = 0; $i < 10000;$i++)
- {
- $data[] = range( 1,100);
- }
- $xls = new Excel_XML();
- $xls->addWorksheet('test', $data);
- $xls->writeWorkbook('test.xls', './');
- }else{
- $data = function($n = 10000){
- for($i = 0; $i < $n; $i++)
- {
- yield xrange(1,100);
- }
- };
- $ xls = new Excel_XML();
- $xls->addWorksheet('test', $data());
- $xls->writeWorkbook('test.xls', './');
- }
- $ctime = microtime(true) - $stime;
- $m = memory_get_usage(true);
- echo "n" . $m / 1014 /1024 . "Mn";
- echo "cost time:" . $ctime . "sn";
- }
-
Copy code
ps: Please refer to this gist for the content of php-excel.class.php file
php-excel.php
New finally keyword
Sample code:
- function getLines($file)
- {
- $f = fopen($file, 'r');
- try
- {
- while ($line = fgets($f))
- {
- yield $line;
- }
- } finally {
- fclose($f);
- }
- }
- foreach(getLines('finally.php') as $v)
- {
- echo $v;
- }
Copy Code When writing code, you must develop good habits and release open resources in a timely manner
password API
The password series functions store passwords for us, realizing a simpler and safer way
Password storage changes from plain text to md5 to md5+salt to mcrpty+salt
password_hash uses the bcrypt encryption algorithm by default to automatically generate salt and encrypt the password
Sample code
- $pw1 = '123456';
- $pwdb = password_hash($pw1, PASSWORD_DEFAULT);
- var_dump($pwdb) . "n";
- var_dump(password_verify($pw1, $pwdb) ) . "n";
- $pw2 = '778920';
- $pwdb = password_hash($pw2, PASSWORD_DEFAULT);
- var_dump($pwdb) . "n";
- var_dump(password_verify($pw1, $pwdb)) . "n";
Copy code
It is strongly recommended that new application development use the built-in password series functions for password storage
array_column
This function should have been there a long time ago
We fetch 10 records from the database. If we want to get a specific column among these ten records, we could only use foreach before.
Now we only need an array_coulum function to do it. This function is of great practical significance and is discussed separately
Sample code:
- // Array representing a possible record set returned from a database
- $records = array(
- array(
- 'id' => 2135,
- 'first_name' => 'John',
- 'last_name' => 'Doe',
- ),
- array(
- 'id' => 3245,
- 'first_name' => 'Sally',
- 'last_name' => 'Smith',
- ) ,
- array(
- 'id' => 5342,
- 'first_name' => 'Jane',
- 'last_name' => 'Jones',
- ),
- array(
- 'id' => 5623,
- 'first_name' => 'Peter',
- 'last_name' => 'Doe',
- )
- );
- $first_names = array_column($records, 'first_name');
- print_r($first_names);
-
Copy code
familiar
foreach now supports list()
-
- $arr= array(
- array(1,2,3),
- array(1,2,3),
- array(1,2,3),
- );
- foreach( $arr as list($a, $b, $c))
- {
- //todo
- }
-
Copy code
It should be noted that the number of sub-arrays of the traversed array must be the same
empty() supports any expression, and empty can also be used in function calls
Understand
Improved GD
Flip support using new imageflip() function
Advanced cropping supports using imagecrop() & imagecropauto() functions
WebP reading and writing support imagecreatefromwebp() & imagewebp() respectively.
In addition, there are quite a lot of new functions, new classes, new interfaces, and new global constants in PHP5.4 and PHP5.5
If you want to know more, you can check the link below
http://www.php.net/manual/zh/migration55.new-functions.php
http://www.php.net/manual/zh/migration55.classes.php
http://www.php.net/manual/zh/migration55.new-methods.php
http://www.php.net/manual/zh/migration55.global-constants.php
http://www.php.net/manual/zh/migration54.functions.php
http://www.php.net/manual/zh/migration54.classes.php
http://www.php.net/manual/zh/migration54.methods.php
http://www.php.net/manual/zh/migration54.global-constants.php
|