I have an object with some protected fields and a method that uses them. The method doesn't do exactly what I need it to do, but I cannot change the original code since I am writing an add-on. Is it somehow possible to extend the class and override the method so that I could call it on predefined objects of the original class? I thought about monkey patching but apparently it is not implemented in php.
4条回答 默认 最新
- dongyan1625 2016-08-03 13:31关注
You can override a method by extending the parent class, initiating the new class instead of the parent class and naming your method exactly the same as the parent method, that was the child method will be called and not the parent
Example:
class Foo { function sayFoo() { echo "Foo"; } } class Bar extends Foo { function sayFoo() { echo "Bar"; } } $foo = new Foo(); $bar = new Bar(); $foo->sayFoo() //Outputs: Foo $bar->sayFoo() //Outputs: Bar
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 对于这个问题的解释说明
- ¥200 询问:python实现大地主题正反算的程序设计,有偿
- ¥15 smptlib使用465端口发送邮件失败
- ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
- ¥15 对于squad数据集的基于bert模型的微调
- ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
- ¥20 steam下载游戏占用内存
- ¥15 CST保存项目时失败
- ¥20 java在应用程序里获取不到扬声器设备
- ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。