PHP code optimization member variable fetch speed comparison

Source: Internet
Author: User
Tags comparison constructor php class php code

  This article mainly introduces the member variables in PHP in 4 ways to obtain the speed of comparison, and detailed analysis of the reasons, the need for friends can refer to the following

There are 4 code examples where do you think they create objects and get the speed ordering of the member variables?   1: Set member variable to public, assign value to member variable by assignment, get variable     copy code as follows: <?php class Foo {    public $id;} $da Ta = new Foo; $data->id = 10; echo $data->id;?>   2: Set the member variable to public, set the value of the member variable by the constructor, and directly get the variable copy code code as follows: <?php class Foo2 { public $id;  public function __construct ($id) {  $this = $id; &nbsp}}   $data = new Foo2 (10); echo $data->id;?>     3: Set the member variable to protected, set the value of the member variable through the constructor, and get the variable by magic method The copy code code is as follows: <?php class Foo3 {&nbs P;protected $id;  public function __construct ($id) {  $this = $id; &nbsp}    public function getId () {  return $this->id; &NBSP} $data = new Foo3 (10); echo $data->getid ();?>     4: Set the member variable to protected, set the value of the member variable by the constructor, and get the variable <?php class Foo4 {  PR by a member method Otected $id;   Public Function __construct ($id) {    $this->id = $id,  }     Public function __get ($k EY) {   return $this->id;  } $data = new Foo4 (10); echo $data->id;?> sorted by speed of execution: 1243 let's see its opcode:1:   code as follows: 1  zend_fetch_class 4  :4   ' Foo ' 2 &nb Sp NEW         $5:4 3  do_fcall_by_name   0           4  assign        ! 0, $5  zend_assign_obj  ! 0, ' ID ' 6  zend_op_data    10 7  fe Tch_obj_r   $0, ' ID ' 8  echo            $9   2:   Code as follows: 1  zend_f Etch_class 4  :10 ' Foo2 ' 2  new               $11:10 3  send_val           4  do_fcall_by_name  1  5  assign        !1 $11 6  fetch_obj_r   $14! 1, ' ID ' 7  echo            $14   3:   Code as follows: 1 & nbsp Zend_fetch_class 4  :15 ' Foo3 ' 2  new       &NBsp    $16:15 3  send_val        10 4  do_fcall_by_name   1           5  assign        ! 2, $16 6  zend_init_method_call! 2, ' getId ' 7  DO_FCA Ll_by_name  0  $20      8  echo           $   4: Code as follows: 1 &NB Sp Zend_fetch_class 4  :21 ' Foo4 ' 2  new            $22:21 3  end_val   &N Bsp     4  do_fcall_by_name  1           5  assign       & nbsp  !3, $22 6  fetch_obj_r    $25!3, ' id ' 7   ECHO      $25     based on OpCo above DE, referring to its corresponding opcode implementation in the Zend_vm_execute.h file, what can we find?   PHP The process of creating an object is divided into three steps:   Zend_fetch_class gets the variable of the storage class based on the class name, in fact, it initializes the object with a lookup operation of Hashtalbe EG (class_table), which will ex ( Call)->FBC point to the constructor pointer. Call the constructor, and its invocation is the same as other function calls, calling Zend_do_fcall_common_helper_spec &NBSp Second, the Magic method of the call is triggered by conditions, not direct calls, such as our example, the member variable ID get   (zend_std_read_property), the step is: Get the object's properties, if it exists, go to step two; if there are no related properties, go to step three From the object's properties to find whether there exists an attribute corresponding to the name, if there is a return result, if it does not exist, take the third step if there is a __get magic method, call this method to get the variable, if it does not exist, the error returns to the problem of sorting:   What is the difference between the first and the second?   the second opcode less than the first, but slower than the first, because the constructor has a lot of parameters, one more parameter processing opcode. Parameter processing is a relatively time-consuming operation, when we do code optimization, some unnecessary parameters can be removed; When a function has multiple parameters, it is possible to consider wrapping it through an array and passing it in.   Second, why is the third slowest?   because its get parameter is essentially a call to an object member method, the invocation cost of the method is higher than the gain of the variable   Third, why is the fourth faster than the third?   Because the operation of the fourth essentially gets the variable, but its internal implementation of the Magic method calls, relative to the user-defined method, the internal function of the call efficiency will be high. So don't reinvent the wheel when we have some methods that the PHP kernel implements can be invoked. Why is the fourth one slower than the second? Because in the process of getting a variable in a PHP object, when the member variable is not in the definition of the class, it will invoke the PHP-specific magic method __get, one more time to invoke the Magic method.   Summary:   1. Use PHP built-in function 2. It's not a thing. object-oriented (OOP), object-oriented often expensive, each method and object invocation consumes a lot of memory. 3. Try to use less magic methods-unless necessary, do not use the framework, because the framework has a lot of magic methods to use. 4. In performance-first scenarios, it's a good idea to use member variables when you need OOP. 5. Can use PHP syntax structure do not use the function, can use the built-in function does not write, can use the function not to use the object  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.