Perhaps a more modern answer for 2016, it looks like there are now at least 3 options:
Option 1. Using runkit
Link: http://php.net/manual/en/function.runkit-class-emancipate.php
bool runkit_class_emancipate ( string $classname )
"Convert an inherited class to a base class, removes any method whose scope is generic"
Although I have not tested this myself, this seems like a viable option if you have control over the downloaded extensions. The disadvantage here is that you lose all the methods of your ancestors.
Option 2. Using runkit again
Link: http://php.net/manual/en/function.runkit-method-redefine.php
bool runkit_method_redefine ( string $classname , string $methodname , string $args , string $code [, int $flags = RUNKIT_ACC_PUBLIC ] )
"Dynamically changes the code for this method"
This solves the problem of option 1 if your goal is to set up a method or two in the base class.
Option 3: Implementing the Autoloader
Link: http://php.net/manual/en/function.spl-autoload-register.php
bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )
"Register the given function as an implementation of __autoload ()"
This is personally my favorite of 3, because:
- It works in modern versions of PHP
- It can work together with other autoloaders
- This allows you to implement conditional logic around your overrides.
This is also the one for which I have some experience. An example implementation is as follows:
// Some early point in your application // Ideally after other autoloaders have registered spl_autoload_register('autoload_override'); function autoload_override($class) { if ($class == 'TargetClassName') { include '/path/to/overriding/class.php'; } }
Rick buczynski
source share