Problems with class inheritance

Source: Internet
Author: User
Tags php error
Today, we have an interesting question, directly on the code
has a parent class and a subclass
fatherclass.php:
class Father{public $a; function __construct () {$this->a = 3;}}

son.php:
Include ' fatherclass.php '; $son = new Son (); $son-Sonfun (); class Son extends Father{function Sonfun () {echo 222;}}

Run son.php error can not find son this class, the problem came.
1, if the son inherits the parent class extends father to remove, then runs correctly;
2, if the contents of the Fatherclass directly replace the include statement, run correctly;
3. If you write as below, the operation is correct.
Include ' fatherclass.php '; class son extends Father{function Sonfun () {echo 222;}} $son = new Son (), $son-Sonfun ();

Which great God can explain?


Reply to discussion (solution)

The name of the Father class changes fatherclass to Father.

Include ' father.php ';

Please follow the basic principles of first declaration, after use
As an exception: When a class is defined and used in a file, the definition and use are not sequential

Here http://www.laruence.com/2008/08/24/427.html

The PHP script is first compiled into opcode and then executed
The author of the 3 floor article seems to be a PHP Daniel ... I've seen some source-level analysis, and I'm impressed with his avatar.
He explained the compiler mechanism of class inheritance and multiple inheritance bug, can solve the landlord's first question

2, if the contents of the Fatherclass directly replace the include statement, run correctly;
This description of the include (Require?xxxxxx_once) is run during the execution period, at compile time no bird it

In summary, Zend Read the PHP script, if the discovery of a clean and non-inheriting class, it is established (close to the assembly of the ' class ' structure rather than PHP's ' class '), if there is an inherited class and his parent class already exists, the same is established, the other code continues to compile. The php script then becomes opcode. Run code sequentially, such as include read other PHP scripts (opcode), complex inheritance classes, the stage of executing the sink encoding does not intelligently identify the class definition, only strictly in the order of PHP code execution.
If my idea is right, I can explain the landlord's question.

There is a son first, then there is Lao Tzu. It's just a violation.

You do not follow the common sense, but also the PHP bug, it is a bit too much
If PHP does a lot of scanning and slows down, you should say PHP is too slow.

There is a son first, then there is Lao Tzu. It's just a violation.

You do not follow the common sense, but also the PHP bug, it is a bit too much
If PHP does a lot of scanning and slows down, you should say PHP is too slow.



Not so much a bug as "not human enough".
In order to adapt to a variety of design patterns, I think the compilation phase of intelligence is very good AH ~
Even later versions of PHP may be able to do the HTML syntax interpreter as intelligent completion of high fault tolerance
PHP provides opcode cache extension, not only the lexical interpretation of multi-line scanning, delay the performance of the compilation mechanism also has a way to solve ~ More eliminate the OOP of multi-file disk IO cost

Understand? is a php5 bug, if you want to solve it, you can see 3. The address of the great God.

? procedures, according to the moderator, first, Ming, and then use the original?

Thank you for answering questions, usually write code will not be instantiated after the definition, just run an SDK when the small problem found

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