Class inheritance issues

Source: Internet
Author: User
Class inheritance issues today encountered an interesting problem, directly on the code
There is 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;}}

The son class cannot be found when an error occurs when running son. php.
1. if The extends father of the son inherited parent class is removed, the operation is correct;
2. if the content in fatherClass is directly replaced by the include statement, the operation is correct;
3. if it is written as below, it runs correctly
include 'fatherClass.php';class son extends father{function sonFun(){echo 222;}}$son = new son();$son -> sonFun();

Which of the following experts can explain?


Reply to discussion (solution)

Change the fatherclass name to father.

Include 'father. php ';

Please followDeclare first, and then useBasic principles
As an exception: when a class is defined and used in the same file, the definition and use are in no particular order.

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 article on the third floor seems to be a php Daniel. I have read some source code-level analyses and have an impression on his profile picture.
He explained the compilation mechanism of class inheritance and the BUG of multi-inheritance, which can solve the first question of the landlord.

2. if the content in fatherClass is directly replaced by the include statement, the operation is correct;
This description includes (require? Xxxxxx_once ?) It runs only during the execution period, and there is no bird in it during compilation.

To sum up, after zend reads the php script, if it finds a clean class without inheritance, it is first established (close to the 'class' structure of the Assembly rather than the 'class' of php '), if an inherited class exists and its parent class is created, other code continues to be compiled. The php script becomes opcode. Code is run in sequence during execution, such as include reading other php scripts (opcode), complex inheritance class establishment, and the class definition is not intelligently recognized during the execution of assembly code, only php code is executed in strict order.
If my thoughts are correct, I can explain my questions.

I have a son first, and I have a son later. It is against common sense.

If you do not handle things in common sense, or say it's a php bug, it's a little too much.
If php performs multiple scans to reduce the speed, you should say php is too slow.

I have a son first, and I have a son later. It is against common sense.

If you do not handle things in common sense, or say it's a php bug, it's a little too much.
If php performs multiple scans to reduce the speed, you should say php is too slow.



It is not so much a bug as "not user-friendly"
In order to adapt to various design patterns, I think the compilation stage is intelligent ~
Even later versions of php may achieve smart completion of high fault tolerance like the html syntax interpreter
Php provides opcode cache extensions, not only the lexical interpretation of multiline scanning, but also the compilation mechanism that delays performance ~ This eliminates the disk IO overhead caused by multiple OOP files.

Understanding? Is it php5? Bug. if you want to solve it ?, 3? The address of the great god.

? Program ?, By moderator? First? Ming, then use the original ?.

Thank you for your questions. at ordinary times, writing code won't be instantiated and then defined. it's just a small problem found when running an SDK.

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.