Home  >  Article  >  Backend Development  >  New features in PHP 5.3 migration to higher versions

New features in PHP 5.3 migration to higher versions

WBOY
WBOYOriginal
2016-07-25 08:46:311007browse
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:
  1. trait Hello {
  2. public function sayHello() {
  3. echo 'Hello ' . "n";
  4. }
  5. }
  6. trait World {
  7. public function sayWorld() {
  8. echo 'World' . "n";
  9. }
  10. }
  11. class MyHelloWorld {
  12. use Hello, World;
  13. public function sayExclamationMark() {
  14. echo '!' . "n";
  15. }
  16. }
  17. $o = new MyHelloWorld();
  18. $ o->sayHello();
  19. $o->sayWorld();
  20. $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:

  1. trait A {
  2. public function smallTalk() {
  3. echo 'a';
  4. }
  5. public function bigTalk() {
  6. echo 'A';
  7. }
  8. }
  9. trait B {
  10. public function smallTalk() {
  11. echo 'b';
  12. }
  13. public function bigTalk() {
  14. echo 'B';
  15. }
  16. }
  17. class Talker {
  18. use A, B {
  19. B::smallTalk instead of A;
  20. A ::bigTalk instead of B;
  21. }
  22. }
  23. class Aliased_Talker {
  24. use A, B {
  25. B::smallTalk instead of A;
  26. A::bigTalk instead of B;
  27. B::bigTalk as talk;
  28. }
  29. }
Copy code
New short array syntax
  1. $a = [1, 2, 3, 4];
  2. $a = ['one' => 1, 'two' => 2, 'three' => 3 , 'four' => 4];
Copy code
Added support for member access analysis of arrays returned by functions
  1. function foo()
  2. {
  3. return array(1,3,4,5);
  4. }
  5. $var =foo()[0];
Copy code

Now regardless of whether the short_open_tag php.ini option is set or not,

Added access to class members during instantiation

  1. class Test
  2. {
  3. public function foo()
  4. {
  5. //todo
  6. return 1;
  7. }
  8. }
  9. $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:
  1. class Utils
  2. {
  3. public static function test1()
  4. {
  5. echo 1;
  6. }
  7. public static function test2()
  8. {
  9. echo 2;
  10. }
  11. }
  12. $m = ' test';
  13. Utils::{$m . (10-8)}();
  14. 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
  1. --enabled-opcache
  2. Recommended configuration (php.ini)
  3. zend_extension=opcache.so
  4. opcache.enable_cli=1
  5. opcache.memory_consumption=128 //Shared memory size, this can be adjusted according to your needs
  6. opcache .interned_strings_buffer=8 //The memory size of interned string can also be adjusted
  7. opcache.max_accelerated_files=4000 //The maximum number of cached files
  8. opcache.revalidate_freq=60 //Check file updates every 60s
  9. opcache.fast_shutdown=1 //Open Quick shutdown, when opening this in PHP Request Shutdown, the speed of memory recovery will be increased
  10. 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.
  1. ini_set('memory_limit', '512M');
  2. $cmd = isset($argv[1]) ? (int)$argv[1] : 0;
  3. function xrange($ start, $end, $step = 1)
  4. {
  5. if($start <= $end)
  6. {
  7. if($step <= 0)
  8. {
  9. throw new LogicException('step must be +ve') ;
  10. }
  11. for($i = $start; $i <= $end; $i = $i + $step)
  12. {
  13. yield $i;
  14. }
  15. }else{
  16. if($step >= 0)
  17. {
  18. throw new LogicException('step must be -ve');
  19. }
  20. for($i = $start; $i >= $end; $i = $i + $step)
  21. {
  22. yield $i;
  23. }
  24. }
  25. }
  26. if($cmd == 0)
  27. {
  28. $r = range(1, 1000000);
  29. foreach($r as $v)
  30. {
  31. if($v > 20)
  32. {
  33. break;
  34. }
  35. echo "$vt";
  36. }
  37. $m = memory_get_usage(true);
  38. echo "n" . $m/1014/1024 . "Mn";
  39. }else if($ cmd == 1){
  40. $r = xrange(1, 1000000);
  41. foreach($r as $v)
  42. {
  43. if($v > 20)
  44. {
  45. break;
  46. }
  47. echo "$vt" ;
  48. }
  49. $m = memory_get_usage(true);
  50. echo "n" . $m / 1014 /1024 . "Mn";
  51. }else{
  52. include 'php-excel.class.php';
  53. $stime = microtime (true);
  54. if($cmd == 3)
  55. {
  56. $data = array();
  57. for($i = 0; $i < 10000;$i++)
  58. {
  59. $data[] = range( 1,100);
  60. }
  61. $xls = new Excel_XML();
  62. $xls->addWorksheet('test', $data);
  63. $xls->writeWorkbook('test.xls', './');
  64. }else{
  65. $data = function($n = 10000){
  66. for($i = 0; $i < $n; $i++)
  67. {
  68. yield xrange(1,100);
  69. }
  70. };
  71. $ xls = new Excel_XML();
  72. $xls->addWorksheet('test', $data());
  73. $xls->writeWorkbook('test.xls', './');
  74. }
  75. $ctime = microtime(true) - $stime;
  76. $m = memory_get_usage(true);
  77. echo "n" . $m / 1014 /1024 . "Mn";
  78. echo "cost time:" . $ctime . "sn";
  79. }
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:
  1. function getLines($file)
  2. {
  3. $f = fopen($file, 'r');
  4. try
  5. {
  6. while ($line = fgets($f))
  7. {
  8. yield $line;
  9. }
  10. } finally {
  11. fclose($f);
  12. }
  13. }
  14. foreach(getLines('finally.php') as $v)
  15. {
  16. echo $v;
  17. }
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
  1. $pw1 = '123456';
  2. $pwdb = password_hash($pw1, PASSWORD_DEFAULT);
  3. var_dump($pwdb) . "n";
  4. var_dump(password_verify($pw1, $pwdb) ) . "n";
  5. $pw2 = '778920';
  6. $pwdb = password_hash($pw2, PASSWORD_DEFAULT);
  7. var_dump($pwdb) . "n";
  8. 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:
  1. // Array representing a possible record set returned from a database
  2. $records = array(
  3. array(
  4. 'id' => 2135,
  5. 'first_name' => 'John',
  6. 'last_name' => 'Doe',
  7. ),
  8. array(
  9. 'id' => 3245,
  10. 'first_name' => 'Sally',
  11. 'last_name' => 'Smith',
  12. ) ,
  13. array(
  14. 'id' => 5342,
  15. 'first_name' => 'Jane',
  16. 'last_name' => 'Jones',
  17. ),
  18. array(
  19. 'id' => 5623,
  20. 'first_name' => 'Peter',
  21. 'last_name' => 'Doe',
  22. )
  23. );
  24. $first_names = array_column($records, 'first_name');
  25. print_r($first_names);
Copy code

familiar

foreach now supports list()
  1. $arr= array(
  2. array(1,2,3),
  3. array(1,2,3),
  4. array(1,2,3),
  5. );
  6. foreach( $arr as list($a, $b, $c))
  7. {
  8. //todo
  9. }
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

Higher, new features, PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn