Redefining PHP Function

Redefining PHP function?

Nope, that throws an error:

Fatal error: Cannot redeclare foo()

The runkit provides options, including runkit_function_rename() and runkit_function_redefine().

Redefine Built in PHP Functions

runkit_function_redefine — Replace a function definition with a new implementation

Note: By default, only userspace functions may be removed, renamed, or
modified. In order to override
internal functions, you must enable
the runkit.internal_override setting
in php.ini.

Redefining PHP class functions on the fly?

Here's an idea of a possible solution you could try. Let the Cow and Human classes extend the Entity class. However, the Entity class would use a factory to instantiate the objects based on if the value was safe. Let's look at this in more detail:

/*
* Class Entity should not be able to be instantiated.
* It should contain a factory to instantiate the
* appropriate entity and an abstract function declaring
* the method that each entity will need to implement.
*/
abstract class Entity {

public static function factory($type) {
return (is_subclass_of($type, "Entity")) ? new $type() : FALSE;
}

abstract public function sayHi();

}

/*
* Human class extends Entity and implements the
* abstract method from Entity.
*/
class Human extends Entity {

public function sayHi() {
echo "Hello World!";
}

}

/*
* Cow class extends Entity and implements the
* abstract method from Entity.
*/
class Cow extends Entity {

public function sayHi() {
echo "Moo!";
}

}

Now to use this method, call the factory method and if all works well, it'll instantiate the proper class which will extend Entity.

$person = Entity::factory("Human");
$person->sayHi();

$cow = Entity::factory("Cow");
$cow->sayHi();

Using, is_subclass_of() will keep you safe because if the passed in value is not a class that extends Entity, you'll be returned a value of FALSE.

If you'd like to see the above code in action, copy the above php code and test it out on phpfiddle.org.

Redefine Class Methods or Class

It's called monkey patching. But, PHP doesn't have native support for it.

Though, as others have also pointed out, the runkit library is available for adding support to the language and is the successor to classkit. And, though it seemed to have been abandoned by its creator (having stated that it wasn't compatible with PHP 5.2 and later), the project does now appear to have a new home and maintainer.

I still can't say I'm a fan of its approach. Making modifications by evaluating strings of code has always seemed to me to be potentially hazardous and difficult to debug.

Still, runkit_method_redefine appears to be what you're looking for, and an example of its use can be found in /tests/runkit_method_redefine.phpt in the repository:

runkit_method_redefine('third_party_library', 'buggy_function', '',
'return \'good result\''
);

PHP - override existing function

Only if you use something like APD that extends the zend engine to allow for that:

Intro

Override Method Docs

Note: Runkit seems like a better option than APD since it's more specific to this purpose and would allow you to keep the original method intact at a different address.

Redefine core function in another namespace

You cannot do this for echo, because it's not actually a function. echo is a "language construct" and a reserved word. You cannot name a function echo.

For built-in functions (that are actually functions) however, what you have is right. For example:

namespace test;
function print_r($yo) {
\print_r('--' . $yo);
}
print_r('lol');

Redefining constants in PHP

If you have the runkit extension installed, you can use runkit_constant_redefine():

runkit_constant_redefine("name", "value");

In most circumstances, however, it would be a better idea to re-evaluate why you're using constants and not something more fitting.



Related Topics



Leave a reply



Submit