How PHP finds the object that is being instantiated

Source: Internet
Author: User
Tags parse error
For example, I have a test object, the first instance of the object, and then the object after the instantiation, this time why also find the object?

$test = new Test();echo $test->run();class Test{    public function run(){        return 'run---';    }}

How does the above code work, and how does PHP find the test object?

Reply content:

For example, I have a test object, the first instance of the object, and then the object after the instantiation, this time why also find the object?

$test = new Test();echo $test->run();class Test{    public function run(){        return 'run---';    }}

How does the above code work, and how does PHP find the test object?

You should understand that any interpreter is not directly explaining the source code (although you can do this), but rather after the "preprocessing", after the results of this preprocessing are performed. It's an interpreter, though.
Like you wrote

echo "Hello world";exit;!#$%^&*()error

According to your understanding of the logic, maybe the PHP interpreter is now to start scanning the source code, it will print out Hello world, but give you a PHP Parse error:syntax error, the exception.
Strangely, it can be seen that the source code was checked before it was executed, including but not limited to syntax detection, that the PHP source was processed and that the test you defined already existed. That is to say, the so-called interpreter, basically there is a "pseudo-compilation" process, so-called dynamic refers to, You need to do this every time you request, for Java, these tasks require you to manually specify, compile the OK class, and continue to the virtual machine execution.

Here is the simplification of the execution process found in bird's blog, the interpreter is actually explaining opcode (which is also the basis of APC's existence):

    1. Lexical analysis, remove annotations, blank, get token

    2. parsing, generating opcode array (op_array) in this process

    3. Explanation execution, execution Op_array, one-piece explanation execution Opline (SWITCH, call, GOTO)

PHP Interpreter is a C program.
It is an interpreted language, so there is no need to compile, but this compilation refers to "Compiling binary files".
But from the script to the result, there is a series of processes in between.

The language itself is just more in line with human habits, and the PHP interpreter eventually executes the opcode.

The process from PHP scripts to opcodes is in PHP5:

Lexing: Lexical scan analysis to convert source files into token streams;
Parsing: Parsing, generating op arrays at this stage.

In PHP7, the OP arrays is no longer directly generated in the parsing phase, but Mr. Cheng is an AST, so the process is one step further:

Lexing: Lexical scan analysis to convert source files into token streams;
Parsing: parsing, generating abstract syntax trees from token streams;
Compilation: Generates OP arrays from an abstract syntax tree.

The above lexing, that is, lexical analysis, PHP with re2c,parsing is the syntax analysis, PHP is bison.
Lexing do some symbolic substitutions, state records of things.
Parsing will scan the syntax, and then call the appropriate handler functions, such as zend_do_begin_class_declaration. Of course this is PHP5,PHP7 it will first go to tune Zend_ast_create and the like.

Next, the corresponding processing function of parsing in PHP5 will convert the statement to opcode, the variable exists in memory, the function name, the class name exist in the symbol table.
PHP7 will have compile related functions to parse the abstract syntax tree, and then get the same result as above.

This way your variables, classes, and functions are ready in memory, and your assignment statements and function call statements have become an array of opcode arrays in order.

And then it's executed. There will be a excute-related function to execute the opcode in one piece, and the result is the one you want.

So your class is written down here, but it can be found entirely.

The above-mentioned procedures, the corresponding PHP interpreter files are:

Zend_language_scanner.l
Zend_language_parser.y
ZEND_AST.C (PHP7)
Zend_compile.c
Zend_excute.c

If you have a C-language base, it's easier to try to understand it from another level.

PHP is an interpreted language and does not have to declare function definitions in advance, as in the case of a compiled language (c).

The computer will load the entire PHP code into memory before running. Then go to find the call:
I was a little white for one months. I don't know, right?

The code is placed in an in-memory code area that is called when the program is run, so the order in your code is not related, but it is recommended for easy reading, and the instance is in front.

Don't be happy too early, if it inherits another class (include), it should be placed below, PHP is a wonderful

I think the explanation is only the principle, did not explain the root of the problem, the question is to ask why the class is defined to the call class can also be, in common sense, the class is defined in front of the call can be found.
Let me explain the problem, first of all, define the class, do not call, is meaningless, to invoke, it must be instantiated, the above mentioned opcode execution mechanism, this is right, then I say the reason based on this, that is, PHP in the implementation of the pre-compilation, So when compiling the execution of the data into memory, the way it is stored and we write code is not the same, PHP put each type of data in a different location, such as put the class into the heap, that is, you this class, regardless of the definition in front and back, the execution of the program, The equivalent of ignoring your definition (because it was moved to the heap during the pre-compilation phase), it is only in the execution to instantiate the specific class that it will go to the heap to find out whether the class is defined, defined by the class data defined in the heap to instantiate, and then execute.

  • 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.