Comparison of PHP member variables

Source: Internet
Author: User
There are four sample codes below. What do you think is the sorting speed of the object they create and get the member variables? 1: Set the member variables to public, assign values to the member variables through the assignment operation, directly get the variable classFoopublic $ id; $ datanewFoo; $ data-id10; echo $ data-id; 2: set the member variable to public.

There are four sample codes below. What do you think is the sorting speed of the object they create and get the member variables? 1: Set the member variable to public. assign a value to the member variable through the value assignment operation to directly obtain the variable class Foo public $ id; $ data = new Foo; $ data-id = 10; echo $ data-id; 2: Set the member variable to public.

There are four sample codes below. What do you think is the sorting speed of the object they create and get the member variables?

1: Set the member variable to public. assign a value to the member variable through the value assignment operation to directly obtain the variable.

class Foo {public $id;}$data = new Foo;$data->id = 10;echo $data->id;

2: Set the member variable to public. You can use the constructor to set the value of the member variable and directly obtain the variable.

        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 through the constructor, and get the variable through the member Method

      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 through the constructor, and obtain the variable through magic

      class Foo4 {protected $id;public function __construct($id) {$this->id = $id;}public function __get($key) {return $this->id;}}$data = new Foo4(10);echo $data->id;

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

1:

   1  ZEND_FETCH_CLASS4 :4 'Foo'2  NEW      $5:43  DO_FCALL_BY_NAME0          4  ASSIGN     !0, $55  ZEND_ASSIGN_OBJ!0, 'id'6  ZEND_OP_DATA107  FETCH_OBJ_R$9!0, 'id'8  ECHO        $9

2:

1  ZEND_FETCH_CLASS4 :10'Foo2'2  NEW             $11:103  SEND_VAL        104  DO_FCALL_BY_NAME1 5  ASSIGN    !1, $116  FETCH_OBJ_R$14!1, 'id'7  ECHO        $14

3:

1  ZEND_FETCH_CLASS4 :15'Foo3'2  NEW         $16:153  SEND_VAL     104  DO_FCALL_BY_NAME1          5  ASSIGN     !2, $166  ZEND_INIT_METHOD_CALL!2, 'getId'7  DO_FCALL_BY_NAME0 $20     8  ECHO       $20

4:

1  ZEND_FETCH_CLASS4  :21'Foo4'2  NEW          $22:213  END_VAL      104  DO_FCALL_BY_NAME1          5  ASSIGN        !3, $226  FETCH_OBJ_R  $25 !3, 'id'7   ECHO  $25

Based on the opcode above and its opcode implementation in the zend_vm_execute.h file, what can we find?

I. PHP Kernel Object creation process is divided into three steps:

  1. ZEND_FETCH_CLASS obtains the storage class variables based on the class name and implements a search operation for hashtalbe EG (class_table ).
  2. NEW initializes the object and points EX (call)-> fbc to the constructor pointer.
  3. Call the constructor. The call is the same as that of other functions. They call zend_do_fcall_common_helper_SPEC.

2. The magic method is triggered by a condition instead of a direct call. For example, to obtain the member variable id (zend_std_read_property) in our example, the procedure is as follows:

  1. Obtain the attributes of an object. If the object exists, go to step 2. If there is no relevant attribute, go to step 3.
  2. Check whether the property corresponding to the name exists from the properties of the object. If the returned result exists and does not exist, go to step 3.
  3. If the _ get magic method exists, call this method to obtain the variable. If it does not exist, an error is returned.

Back to sorting:

1. What is the difference between the first and second?

The second opcode is less than the first one, but slower than the first one, because the constructor has more parameters and one more parameter to process opcode. Parameter processing is a time-consuming operation. When code optimization is performed, some unnecessary parameters can be removed. When a function has multiple parameters, you can use an array to encapsulate it and pass it in.

2. Why is the third slowest?

Because its parameter acquisition is essentially a call to the object member method, the call cost of the method is higher than that of the variable.

3. Why is the fourth one faster than the third one?

Because the fourth operation actually obtains the variable, but calls the magic method internally, the call efficiency of the internal function is higher than that of the User-Defined method. Therefore, when some PHP kernel implementation methods can be called, do not reinvent the wheel.

4. Why is the fourth one slower than the second one?

Because when a PHP Object obtains a variable, when the member variable is not defined in the class, it calls PHP's unique magic method _ get, A magic method call is added.

Summary:

  1. Use PHP built-in functions
  2. It is not essential for Object-Oriented (OOP). Object-oriented usually has a high overhead, and each method and object call consumes a lot of memory.
  3. Try to use less magic methods-do not use a framework unless necessary, because the framework has a lot of magic methods to use.
  4. In performance-first application scenarios, setting member variables to public is a good method when you need to use OOP.
  5. If you can use PHP syntax structure, do not use functions. If you can use built-in functions, do not write them by yourself. If you can use functions, do not use objects.

Original article address: PHP member variable acquisition comparison, thanks to the original author for sharing.

Related Article

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.