PHP member variable get contrast (class member variable)

Source: Internet
Author: User

There are 4 code examples, what 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 operation, get variable directly

The code is as follows Copy Code

Class Foo {
public $id;
}

$data = 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 get the variable directly

Class Foo2 {
public $id;
Public function __construct ($id) {
$this->id = $id;
}
}

$data = new Foo2 (10);
Echo $data->id;


3: Set the member variable to protected, set the value of the member variable by the constructor, and get the variable by magic method

The code is as follows Copy Code

Class Foo3 {
protected $id;
Public function __construct ($id) {
$this->id = $id;
}

Public Function getId () {
return $this->id;
}
}
$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 by the Member method

The code is as follows Copy Code

Class Foo4 {
protected $id;
Public function __construct ($id) {
$this->id = $id;
}//www.111cn.net

Public Function __get ($key) {
return $this->id;
}
}
$data = new Foo4 (10);
Echo $data->id;


Sort by speed of execution: 1243
Let's look at the opcode first:

The code is as follows Copy Code

1:

1 zend_fetch_class 4:4 ' Foo '
2 NEW $5:4
3 Do_fcall_by_name 0
4 ASSIGN! 0, $
5 zend_assign_obj! 0, ' id '
6 Zend_op_data 10
7 fetch_obj_r $0, ' id '
8 ECHO $92:

 1  zend_fetch_class 4  :10  ' Foo2 '
 2  new                $11 :10
 3  send_val            10
 4  do_fcall_by_name  1
 5  assign        !1, $11
 6  fetch_obj_r   $14 ! 1, ' id '
 7  echo            $143:

 1  zend_fetch_class 4  :15  ' Foo3 '
 2  new             $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_fcall_by_name  0  $20    
  8  echo           $204:

1 zend_fetch_class 4:21 ' Foo4 '
2 NEW $22:21
3 End_val 10
4 Do_fcall_by_name 1
5 ASSIGN! 3, $22
6 Fetch_obj_r $!3, ' id '
7 ECHO $


According to the opcode above, what can we find by referring to the opcode implementation corresponding to the Zend_vm_execute.h file?

The process of creating objects in the PHP kernel is divided into three steps:

1.zend_fetch_class gets the variable of the storage class based on the class name, and is actually looking for a hashtalbe EG (class_table)
2.NEW initializes the object and points ex (call)->FBC to the constructor pointer.
3. Call the constructor, and its invocation is the same as other function calls, and is called Zend_do_fcall_common_helper_spec
Second, the Magic method of the call is triggered by the condition, not directly called, as in our example, the member Variable ID acquisition (ZEND_STD_READ_PROPERTY), the steps are:

1. Get the properties of the object, if present, go to step two; if there are no related properties, go to step three
2. Find out if there is a property corresponding to the name from the properties of the object, if there is a return result, and if it does not exist, go to step three
3. If there is a __get magic method, call this method to get the variable, if it does not exist, the error
Back to the sort of question:

What is the difference between the first and the second?

The second opcode is less than the first, but slower than the first, because the constructor has a lot of parameters and a opcode with a parameter. 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 it gets a parameter that 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 the fourth one faster than the third one?

Because the operation of the fourth essentially gets the variable, only its internal implementation of the Magic method calls, in contrast 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.

To sum up:

1. Using PHP built-in functions
2. 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 the PHP syntax structure do not use the function, can use the built-in function does not write by oneself, can use the function not to use the object

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.