简体   繁体   中英

Best practice for filing of abstract class and subclasses?

I was wondering what is the best (and real-word) practice for doing an abstract class and subclasses in PHP when it comes to filing.

I have abstract class games , and I was wondering if I should put the subclasses into separate files and require_once('games.class.php'); for separate files or just have each subclass in the abstract class games file?

Thanks!

People generally tend to use one file per class -- mapping the files names to the classes names (this facilitates autoloading ) .


For a quite good example of this, you should take a look to the sources of Zend Framework : there are a great number of classes, with interfaces, abstract classes, multiple-levels of sub-directories and all.

And here is the relevant part of their Coding Standards .

我建议使用自动加载代替require或include,当然每个类一个文件。

you accomplish this in a more future-proof manner you would definately stick to 'one class per file'.

The actual classes could then be included via many ways amongst which the autoloader. One tip: Just name the file/class accordingly:

abstract class GamesAbstract {}
class Games extends GamesAbstract {}

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