dtt27783 2013-09-24 08:41
浏览 54

PHP继承:Child与父级具有相同的类名

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.

  • 写回答

1条回答 默认 最新

  • douchan0523 2013-09-24 08:51
    关注

    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, e.g. 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测