类继承的问题
今天遇到一个有意思的问题,直接上代码
有一个父类和一个子类
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; }}
运行son.php报错找不到son这个类,问题来了。
1、如果把son继承父类的extends father去掉,则运行正确;
2、如果把fatherClass里的内容直接代替include语句,运行正确;
3、如果像下面一样写,运行也正确
include 'fatherClass.php';class son extends father{ function sonFun(){ echo 222; }}$son = new son();$son -> sonFun();
哪位大神可以解释一下?
回复讨论(解决方案)
father类的名字将fatherclass改成father
include 'father.php';
请遵循 先声明,后使用 的基本原则
作为例外:当类定义和使用同在一个文件中时,定义和使用不分先后
这里 http://www.laruence.com/2008/08/24/427.html
php脚本先被编译成opcode,然后执行
3楼文章的作者好像是个php大牛...看过一些源码级分析,对他的头像有印象
他解释了类继承的编译机制和多重继承的BUG,能解决楼主的第一个疑问
2、如果把fatherClass里的内容直接代替include语句,运行正确;
这个说明include(require?xxxxxx_once?)是在执行期才运行的,编译时压根没鸟它
总结下,zend读取php脚本后,如果发现干净的无继承的类,就先建立出来(接近汇编的'类'结构而不是php的'类'),如果发现有继承类并且他的父类已存在,同样建立,其他代码继续编译。这时php脚本变成了opcode。执行时按顺序运行代码,比如include读取其他php脚本(opcode),复杂继承类的建立,执行汇编码的阶段不会智能识别类定义,只会严格按照php代码的顺序执行。
如果我的想法是对的倒是可以解释楼主的疑问
先有儿子,后有老子。本来就是违背常理的
你不按常理办事,还说是 php 的 bug,就有点过分了
如果 php 做多趟扫描而减低了速度,你有该说 php 太慢了
先有儿子,后有老子。本来就是违背常理的
你不按常理办事,还说是 php 的 bug,就有点过分了
如果 php 做多趟扫描而减低了速度,你有该说 php 太慢了
与其说bug不如解释为“不够人性化”
为了适应各种设计模式,我觉得编译阶段智能一点很好啊~
甚至以后的php版本可能做到html语法解释器那样的智能补全高容错
php提供了opcode缓存扩展,不光是多行扫描的词法解释,再耽误性能的编译机制也有办法解决~更消除了OOP的多文件带来的磁盘IO开销
理解?是php5的一?bug,如果要解?,可以看3?大神的地址。
?程序?,按版主?的,先?明,再使用原?。
多谢各位答疑解惑,平时写代码根本不会先实例化后定义,只是在运行一个SDK的时候发现的这个小问题

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In function inheritance, use "base class pointer" and "derived class pointer" to understand the inheritance mechanism: when the base class pointer points to the derived class object, upward transformation is performed and only the base class members are accessed. When a derived class pointer points to a base class object, a downward cast is performed (unsafe) and must be used with caution.

Inheritance error debugging tips: Ensure correct inheritance relationships. Use the debugger to step through the code and examine variable values. Make sure to use the virtual modifier correctly. Examine the inheritance diamond problem caused by hidden inheritance. Check for unimplemented pure virtual functions in abstract classes.

Solving PHP errors: Problems encountered when inheriting parent classes In PHP, inheritance is an important feature of object-oriented programming. Through inheritance, we can reuse existing code and extend and improve it without modifying the original code. Although inheritance is widely used in development, sometimes you may encounter some error problems when inheriting from a parent class. This article will focus on solving common problems encountered when inheriting from a parent class and provide corresponding code examples. Question 1: The parent class is not found. During the process of inheriting the parent class, if the system does not

Inheritance is a concept that allows us to access the properties and behavior of one class from another class. The class that inherits methods and member variables is called a superclass or parent class, and the class that inherits these methods and member variables is called a subclass or subclass. In Java, we use "extends" keyword to inherit a class. In this article, we will discuss a Java program to calculate interest on fixed deposits and time deposits using inheritance. First, create these four Java files - Acnt.java − in your local machine IDE. This file will contain an abstract class ‘Acnt’ which is used to store account details like interest rate and amount. It will also have an abstract method 'calcIntrst' with parameter 'amnt' for calculating

Detailed explanation of C++ function inheritance: Master the relationship between "is-a" and "has-a" What is function inheritance? Function inheritance is a technique in C++ that associates methods defined in a derived class with methods defined in a base class. It allows derived classes to access and override methods of the base class, thereby extending the functionality of the base class. "is-a" and "has-a" relationships In function inheritance, the "is-a" relationship means that the derived class is a subtype of the base class, that is, the derived class "inherits" the characteristics and behavior of the base class. The "has-a" relationship means that the derived class contains a reference or pointer to the base class object, that is, the derived class "owns" the base class object. SyntaxThe following is the syntax for how to implement function inheritance: classDerivedClass:pu

How to use polymorphism and inheritance to handle data types in PHP Introduction: In PHP, polymorphism and inheritance are two important object-oriented programming (OOP) concepts. By using polymorphism and inheritance, we can handle different data types more flexibly. This article will introduce how to use polymorphism and inheritance to deal with data types in PHP, and show their practical application through code examples. 1. The basic concept of inheritance Inheritance is an important concept in object-oriented programming. It allows us to create a class that can inherit the properties and methods of the parent class.

How to force inheritance of proxy final class using Java? In Java, the final keyword is used to modify classes, methods, and variables, indicating that they cannot be inherited, overridden, or modified. However, in some cases, we may need to force inheritance of a final class to achieve specific needs. This article will discuss how to use the proxy pattern to implement such functionality. The proxy pattern is a structural design pattern that allows us to create an intermediate object (proxy object) that can control the behavior of another object (proxy object).

Encapsulation technology and application encapsulation in PHP is an important concept in object-oriented programming. It refers to encapsulating data and operations on data together in order to provide a unified access interface to external programs. In PHP, encapsulation can be achieved through access control modifiers and class definitions. This article will introduce encapsulation technology in PHP and its application scenarios, and provide some specific code examples. 1. Encapsulated access control modifiers In PHP, encapsulation is mainly achieved through access control modifiers. PHP provides three access control modifiers,
