简体   繁体   English

扩展php中的问题

[英]extends issue in php

i have 2 .php files 我有2个.php文件

1) Parent.php with Class Parent{}
2) Child.php with Class Child{}

I am trying to extend Parent in Child as below: 我试图在Child中扩展Parent,如下所示:

Class Child extends Parent{} // gives error saying Parent not found.

Please help. 请帮忙。

you have to include parent.php file and also change Parent class name. 您必须包含parent.php文件并更改父类名称。

Parent is a keyword so you could try by changing your class name Parent是一个关键字,因此您可以尝试更改您的类名

I hope this will help you. 我希望这能帮到您。

You do not need to include the parent definition in the file which defines child. 不需要在定义child的文件中包含父定义。

Indeed some coding style rules expressly prohibit this. 实际上,一些编码风格规则明确禁止这一点。

The only time you will see the error being reported is at run time - and to fix you only need to ensure that the parent class has been parsed and is in scope before the child class is parsed. 您将在运行时唯一一次看到报告的错误 - 并且为了解决这个问题,您只需要确保在解析子类之前已经解析了父类并且它在范围内。 Including the file defining parent (where permitted by your coding style rules) is one way to accomplish this, however you must use require rather than include (ir use include with a trappable error on failure) and you must use the _once variant to ensure you don't try to load the definition multiple times. 包括定义父文件的文件(在编码样式规则允许的情况下)是实现此目的的一种方法,但是您必须使用require而不是include(如果失败时使用包含可捕获的错误)并且必须使用_once变体来确保不要尝试多次加载定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM