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 function?
Nope, that throws an error:
Fatal error: Cannot redeclare foo()
The runkit provides options, including runkit_function_rename()
and runkit_function_redefine()
.
How to override built-in PHP function(s)?
I think it could be done like so:
//First rename existing function
rename_function('strlen', 'new_strlen');
//Override function with another
override_function('strlen', '$string', 'return override_strlen($string);');
//Create the other function
function override_strlen($string){
return new_strlen($string);
}
found it here
Notice that every host must have http://php.net/manual/en/book.apd.php installed on the server.
Edit
Another way is to use namespaces
<?php
namespace mysql2pdo;
use PDO;
function mysql_connect() {
return new PDO();
}
echo mysql_connect(); // Causes error because we don't have the parameters
?>
Test it here
PHP - runkit redefine method
{... removed as not necessary ...}
==== EDIT =====
[For the new problem, what you need is this]
<?
class start {
function __construct() {
$this->options = array();
}
public function process() {
// some code here
$this->file_not_exists();
}
public function file_not_exists() {
$this->options['page'] = 222;
}
public function redefine($what, $code) {
runkit_method_redefine(get_class($this),
$what,
'',
'call_user_func(array($this,' .$code. '),$this);',
RUNKIT_ACC_PUBLIC);
}
public function my_func($someobj)
{
print_r($someobj);
}
}
$start = new start();
$start->redefine('file_not_exists', 'my_func');
$start->process();
?>
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');
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\''
);
Related Topics
In PHP (≫= 5.0), Is Passing by Reference Faster
PHP: Recreate and Display an Image from Binary Data
Redefine Class Methods or Class
When (If Ever) Is Eval Not Evil
Converting Words to Numbers in PHP
Dollar ($) Sign in Password String Treated as Variable
PHP - How to Force Download of a File
Connecting to Ws-Security Protected Web Service With PHP
How to Create an Error 404 Page Using PHP
Creating a Thumbnail from an Uploaded Image
Convert Plain Text Urls into HTML Hyperlinks in PHP
Are There Good Tutorials on How to Use Pdo
The Character Encoding of the HTML Document Was Not Declared
How to Upload & Save Files With Desired Name
Accessing Outside Variable Using Anonymous Function as Params
Who Should Handle the Conditions in Complex Queries, the Data Mapper or the Service Layer