How to masquerade child classes in parent class with autoloading (php)?

Class ParentClass { public function __construct() { $this->do_stuff(); } } class SpecializedClass extends ParentClass { public function __construct() { // Optional, do the stuff parent does (do_stuff()); parent::__construct(); // ... specialized construction logic here $this->do_other_stuff(); } // ... specialized methods here. } class NormalClass1 extends ParentClass { } class SpecialClass1 extends SpecializedClass { } // ... etc.

My current setup automatically loads a class if it's in a separate file, but it can't load a class if it's defined in a different class' file. – Cat Sep 20 '10 at 23:27 Ah I understand what you're asking here.In my opinion, relying on __autoload functionality is a great way to get a confusing codebase very quickly. You should group your related classes into files and require them where needed manually.

Using require_once is great because if you have circular dependancies, or a complex dependancy chain, you're not going to include the same set of classes more than once. – Sam Day Sep 20 '10 at 23:31.

You'd have to add the logic to the __autoload() function, or an spl_autoload_register(), which by string comparison decides what file to load / if it possibly is one of the children of your parent.

In my opinion it's beneficial to maintain the convention of one class per file. It keeps the autoloader straightforward, facilitates reuse of individual classes and, most importantly, makes the source code easier to navigate and reason about. If you have many classes with common parent, consider moving them to a separate subdirectory and add this directory to autoloader.

This is not necessarily true for languages other than PHP that have better import semantics.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions