开发者

Symfony: trying to overwriting a method of a symfony class

开发者 https://www.devze.com 2023-01-02 13:02 出处:网络
i want to overwrite a method of symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php.

i want to overwrite a method of symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php.

I think a good way could be writing again the 开发者_高级运维method in the form class where i need that method.

In that case if i need that method in other form class should i write again the new method, so i would break the rule DRY...

So is there any better way?

Regards

Javi


You should be using BaseFormDoctrine if this is a Doctrine specific method or BaseForm if you want the method to apply to all forms. These form classes are provided specifically for this purpose.


Piggybacking on Colonel Sponz's answer, I've done this many times by extending the class I want to override. By extending the class, you don't have to duplicate anything. In the method(s) you want to customize, just add your customized code and then call parent::method_name() to execute the same method of the super class. You get all the benefits of both. Calls to methods that don't exist in the subclass will execute against the super class.

It should be noted that this strategy is basic OOP stuff and isn't limited to Symfony or even PHP.


You could create a new class that inherits from sfFormDoctrine that redeclares the methods you need and then use your new class in place of sfFormDoctrine wherever you need that method.

0

精彩评论

暂无评论...
验证码 换一张
取 消