Home php教程 php手册 PHP成员变量获取对比

PHP成员变量获取对比

Jun 06, 2016 pm 08:11 PM
php code variable Compared member Example Obtain

有如下4个代码示例,你认为他们创建对象,并获得成员变量的速度排序是怎样的? 1:将成员变量设置为public,通过赋值操作给成员变量赋值,直接获取变量 class Foo public $id;$data = new Foo;$data-id = 10;echo $data-id; 2:将成员变量设置为public,通过

有如下4个代码示例,你认为他们创建对象,并获得成员变量的速度排序是怎样的?

1:将成员变量设置为public,通过赋值操作给成员变量赋值,直接获取变量

1

2

3

4

5

6

class Foo {

    public $id;

}

$data = new Foo;

$data->id = 10;

echo $data->id;

Copy after login

2:将成员变量设置为public,通过构造函数设置成员变量的值,直接获取变量

1

2

3

4

5

6

7

8

    class Foo2 {

    public $id;

    public function __construct($id) {

        $this->id = $id;

    }

}

$data = new Foo2(10);

echo $data->id;

Copy after login

3:将成员变量设置为protected,通过构造函数设置成员变量的值,通过成员方法获取变量

1

2

3

4

5

6

7

8

9

10

11

class Foo3 {

    protected $id;

    public function __construct($id) {

        $this->id = $id;

    }

    public function getId() {

        return $this->id;

    }

}

$data = new Foo3(10);

echo $data->getId();

Copy after login

4:将成员变量设置为protected,通过构造函数设置成员变量的值,通过魔术方法获取变量

1

2

3

4

5

6

7

8

9

10

11

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;

Copy after login

按执行速度快慢排序: 1243
咱们先看其opcode:

1:

1

2

3

4

5

6

7

8

1  ZEND_FETCH_CLASS 4   :4  'Foo'

2  NEW                  $5  :4

3  DO_FCALL_BY_NAME         0         

4  ASSIGN                   !0, $5

5  ZEND_ASSIGN_OBJ          !0, 'id'

6  ZEND_OP_DATA             10

7  FETCH_OBJ_R          $9  !0, 'id'

ECHO                     $9

Copy after login

2:

1

2

3

4

5

6

7

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'

ECHO                     $14

Copy after login

3:

1

2

3

4

5

6

7

8

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    

ECHO                     $20

Copy after login

4:

1

2

3

4

5

6

7

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          $25 !3, 'id'

7   ECHO                $25

Copy after login

根据上面的opcode,参照其在zend_vm_execute.h文件对应的opcode实现,我们可以发现什么?

一、PHP内核创建对象的过程分为三步:

  1. ZEND_FETCH_CLASS 根据类名获取存储类的变量,其实现为一个hashtalbe EG(class_table) 的查找操作
  2. NEW 初始化对象,将EX(call)->fbc指向构造函数指针。
  3. 调用构造函数,其调用和其它的函数调用是一样,都是调用zend_do_fcall_common_helper_SPEC

二、魔术方法的调用是通过条件触发的,并不是直接调用,如我们示例中的成员变量id的获取(zend_std_read_property),其步骤为:

  1. 获取对象的属性,如果存在,转第二步;如果没有相关属性,转第三步
  2. 从对象的properties查找是否存在与名称对应的属性存在,如果存在返回结果,如果不存在,转第三步
  3. 如果存在__get魔术方法,则调用此方法获取变量,如果不存在,报错

回到排序的问题:

一、第一个和第二个的区别是什么?

第二个的opcode比第一个要少,反而比第一个要慢一些,因为构造函数多了参数,多了一个参数处理的opcode。参数处理是一个比较费时的操作,当我们在做代码优化时,一些不必要的参数能去掉就去掉;当一个函数有多个参数时,可以考虑通过一个数组将其封装后传递进来。

二、为啥第三个最慢?

因为其获取参数其本质上是一次对象成员方法的调用,方法的调用成本高于变量的获取

三、为啥第四个比第三个要快?

因为第四个的操作实质上获取变量,只不过其内部实现了魔术方法的调用,相对于用户定义的方法,内部函数的调用的效率会高。因此,当我们有一些PHP内核实现的方法可以调用时就不要重复发明轮子了。

四、为啥第四个比第二个要慢?

因为在PHP的对象获取变量的过程中,当成员变量在类的定义不在在时,会去调用PHP特有的魔术方法__get,多了一次魔术方法的调用。

总结一下:

  1. 使用PHP内置函数
  2. 并不是事必面向对象(OOP),面向对象往往开销很大,每个方法和对象调用都会消耗很多内存。
  3. 尽量少用魔术方法 — 除非有必要,不要用框架,因为框架都有大量的魔术方法使用。
  4. 在性能优先的应用场景中,将成员变量设置为public,不失为一种比较好的方法,当你需要用到OOP时。
  5. 能使用PHP语法结构的不要用函数,能使用内置函数的不要自己写,能用函数的不要用对象
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles