Table of Contents
回复内容:
Home Backend Development PHP Tutorial php是如何找到要实例的对象的

php是如何找到要实例的对象的

Jun 06, 2016 pm 08:23 PM
php

比如我有个test对象,首先先实例对象,然后对象在实例化的后面,这个时候为什么还会找的到对象?

<code>$test = new Test();
echo $test->run();

class Test{
    public function run(){
        return 'run---';
    }
}
</code>
Copy after login
Copy after login

上面代码是怎么运行的,php怎么找的到test对象的?

回复内容:

比如我有个test对象,首先先实例对象,然后对象在实例化的后面,这个时候为什么还会找的到对象?

<code>$test = new Test();
echo $test->run();

class Test{
    public function run(){
        return 'run---';
    }
}
</code>
Copy after login
Copy after login

上面代码是怎么运行的,php怎么找的到test对象的?

你应该明白任何解释器都不是直接解释源代码的(虽然你可以这么做),而是经过"预处理",之后,在执行这个预处理的结果.虽然说是解释器
比如你写

<code>echo "Hello world";
exit;
!#$%^&*()error</code>
Copy after login

按照你理解的逻辑,可能php的解释器是现在才开始扫描这个源码,它会打印出Hello world的,但却给你一个PHP Parse error: syntax error, 的异常.
奇怪,可见,源代码在执行前经过了一次检查,这些检查包括但不限于语法检测,php的源码是经过处理的,你所定义的Test已经存在了.也就是说,所谓的解释器,基本上都存在"伪编译"的过程,所谓动态是指,你每一次请求都需要经过这样的处理,对Java而言,这些工作需要你手动指定,编译OK的class,继续交给虚拟机执行.

下面是在鸟哥的博客里面找到的执行流程简化,解释器其实是在解释Opcode(这也是APC存在的基础):

  1. 词法分析, 去掉注释, 空白, 得到TOKEN

  2. 语法分析, 在这个过程中生成Opcode array (op_array)

  3. 解释执行, 执行op_array, 一条一条的解释执行Opline(SWITCH, CALL, GOTO)

PHP 解释器是个 C 程序。
它是解释型语言,所以无需编译,但这个编译指的是『编译成二进制文件』。
但是从脚本到结果,中间还是有一系列过程的。

语言本身只是为了更符合人的习惯,PHP 解释器最终执行的是 opcode。

从从 php 脚本到 opcodes 的的过程在 PHP5 中是:

Lexing:词法扫描分析,将源文件转换成 token 流;
Parsing:语法分析,在此阶段生成 op arrays。

PHP7 中在语法分析阶段不再直接生成 op arrays,而是先生成 AST,所以过程多了一步:

Lexing:词法扫描分析,将源文件转换成 token 流;
Parsing:语法分析,从 token 流生成抽象语法树;
Compilation:从抽象语法树生成 op arrays。

上面的 lexing,也就是词法分析,PHP 用的是 re2c,parsing 也就是语法分析,PHP 用的是bison。
lexing 做一些符号替换,状态记录的东西。
parsing 会去扫描语法,然后调用相应的处理函数,比如 zend_do_begin_class_declaration 之类的函数。当然这是 PHP5,PHP7 它会先去调 zend_ast_create 之类的。

接下来 PHP5 中 parsing 调的相应的处理函数就会将语句转换成 opcode,将变量存在内存中,函数名、类名存在符号表中。
PHP7 中会有 compile 相关的函数去分析抽象语法树,然后得到和上面相同的结果。

这样你的变量、类、函数都在内存中准备就绪了,你的赋值语句、函数调用语句都已经变成了顺序排列的一条一条的 opcode array了。

然后就是执行了。会有一个 excute 相关的函数来一条一条的执行 opcode,得到的就是你要的结果。

所以你的类虽然写在下面,但是完全可以被找到的。

上面说的这些过程,对应的 PHP 解释器中的文件分别是:

zend_language_scanner.l
zend_language_parser.y
zend_ast.c (PHP7)
zend_compile.c
zend_excute.c

如果有 C 语言的底子,尝试从另外一个层面去了解会更简单。

php是解释型语言, 不用像编译型语言(c)那样提前声明函数定义。

运行之前电脑会把整个php代码加载到内存中吧。然后再去查找调用。。
我是才学一个月的小白。。不知道对不对

代码会放在内存中的代码区,程序运行的时候调用,所以在你代码中的顺序没有关系,但建议便于阅读,实例放在前面。

别高兴得太早,如果它继承另一个类(include),就要放在下面了,php是个奇葩

我觉得大家解释的都仅仅是原理,没有解释道这题的根本,这道问题的意思是问为什么把类定义到调用类的后面也可以,按照常理来说是类定义在要调用的前面才能查找得到。
我来解释一下这个问题吧,首先,定义了类,不调用,是没有意义的,要调用,就必须要实例化,上面提到的opcode执行机制,这个是对的,那么我说的原因基于这个,也就是说php在执行的时候进行预编译,那么编译的时候把执行的数据放到内存,其存放方式和我们写代码的时候是不一样的,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 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

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles