I’m using WordPress as a CMS, and I want to extend one of its classes without having to inherit from another class; i.e. I simply want to “add” more methods to that class:
class A {
function do_a() {
echo 'a';
}
}
then:
function insert_this_function_into_class_A() {
echo 'b';
}
(some way of inserting the latter into A class)
and:
A::insert_this_function_into_class_A(); # b
Is this even possible in tenacious PHP?
If you only need to access the Public API of the class, you can use a Decorator:
Then wrap the instance of SomeClass:
If you need to have access to the
protectedAPI, you have to use inheritance. If you need to access theprivateAPI, you have to modify the class files. While the inheritance approach is fine, modifiying the class files might get you into trouble when updating (you will lose any patches made). But both is more feasible than using runkit.