简体   繁体   中英

extends issue in php

i have 2 .php files

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

I am trying to extend Parent in Child as below:

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 is a keyword so you could try by changing your class name

I hope this will help you.

You do not need to include the parent definition in the file which defines 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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