简体   繁体   中英

PHP Inheritance: Child has same class name as parent

I am interested if its possible with any trickery, and or cool PHP features to have a child class inherit from a parent and have the same name as a parent. This should be able to be repeated as many times as desired.

This can be done in a number of ways I figure, but I am interested to see what the community turns up.

I am interested to also hear if people think this is useful or if standard inheritance is just as acceptable.

Really the power that I see in it, (assuming the base object is built correctly). is amazingly long term extend ability and maintainability, while being super pro and also not making your average bear confused to all living heck.

Lets pretend we have some active record base types.

Class Active Record => Contacts

I want to Override Contacts with MyContacts but would rather see everywhere in the system currently using Contacts to not have to be changed in anyway whatsoever... and Instead be branded as Contacts.

Active Record -> Contacts -> MyContacts (Contact).

Theoretically as above stated it should be possible to do.

Active Record -> Contacts -> MyContacts (Contact ) -> YourContacts (Contact).

Anywhere Contacts is referenced should still not give any Undefined class, or not give the child's most functionality whenever present.

How could this be achieved without doing something like...

$class_name = ClassFactory::instance("Contact");
$class = new $class_name($args);

What we are looking for: $class = new Contacts();

Where $class could be the newest version of Contacts


Active Record => Contacts
Active Record => Contacts -> MyContacts (Contact)
Active Record => Contacts -> MyContacts (Contact) -> YourContacts (Contact).

In the case of

$class->undefinedFun();

Accepted that -> Absolute base class, or __call function of any of the teirs of the class would define if it is an error or not.

What you are looking for is interfaces . Define your Contact as an Interface and then use that in your application. That way you don't have to change all your code but just the code that creates a specific instance:

interface Contact
{
    public function doSomething();
}

class Whatever extends Something implements Contact
{
    public function doSomething()
    {
        // do something in a particular way
    }
}

class SomethingUsingContacts
{
    public function useContact(Contact $contact)
    {
        $contact->doSomething();
    }
}

If you do it this way, you don't have to rewrite all your clients but can simply swap out the concrete implementation, eg you can do

$somethingUsingContacts = new SomethingUsingContacts;
$somethingUsingContacts->useContact(new Whatever);

and when you don't Whatever anymore, you can just

$somethingUsingContacts->useContact(new OtherWhatever);

and it will work as long as what you inject there implements the Contact interface. Using the Factory approach you want to avoid is perfectly suited and maintainable for this. It concentrates the logic to assemble your object graph in one place and then simply pushes the right objects to the consumers.

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