How to Override Built-In PHP Function(S)

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

Override default php function

You can use namespaces to override existing function names:

namespace blarg;
function basename() {
return 'whatever';
}
$base = basename();

I.e., any call to basename() within the blarg namespace will use your new version of the function.

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.

How to override built-in functions in PHP?

__toString() is what you are looking for.

http://www.php.net/manual/en/language.oop5.magic.php#object.tostring

Is it possible to overwrite a function in PHP

Edit

To address comments that this answer doesn't directly address the
original question. If you got here from a Google Search, start here

There is a function available called override_function that actually fits the bill. However, given that this function is part of The Advanced PHP Debugger extension, it's hard to make an argument that override_function() is intended for production use. Therefore, I would say "No", it is not possible to overwrite a function with the intent that the original questioner had in mind.

Original Answer

This is where you should take advantage of OOP, specifically polymorphism.

interface Fooable
{
public function ihatefooexamples();
}

class Foo implements Fooable
{
public function ihatefooexamples()
{
return "boo-foo!";
}
}

class FooBar implements Fooable
{
public function ihatefooexamples()
{
return "really boo-foo";
}
}

$foo = new Foo();

if (10 == $_GET['foolevel']) {
$foo = new FooBar();
}

echo $foo->ihatefooexamples();

Is it possible to override a default PHP function?

It is possible, using the Runkit extension.

However, it's generally not considered a good idea, except for use with things like unit testing, where you may want to isolate some of your functionality.

For general use, you shouldn't be overriding built-in functions because it makes your code harder to maintain, and opens you up to some very hard to debug issues.

Also, the Runkit extension is marked as 'experimental', which means it really shouldn't be used in a production system.

Can I override the PHP built-in function echo()?

echo is not a function, but a language statement. It cannot be redefined. If you are looking to prettify your output markup, have look at Tidy.


What you could do, is use your IDE's search/replace method and replace all echo statements with echo PHP_EOL,. This would append the OS specific newline char(s) before any output. Note the comma after PHP_EOL as it is important.

You can output several values with echo like this:

echo 'one', $foo, PHP_EOL,
'two', $bar, PHP_EOL;

so there is no need to write echo on each line.

However, I agree with anyone who suggested using a more dedicated approach to separate content and layout e.g. using template views or HereDoc.

In additon, there is very little gain in having pretty markup. If you are using tools like Firebug to inspect the HTML, you will have properly formatted markup regardless of the mess the markup really is. Moreover, on sites with a lot of visitors, you'll often find the markup minified, which is the opposite of what you are trying to do, simply because all these newlines and tabs add to the weight of the page, which leads to slower page loads and increased traffic cost.

Can I Extend a Built-In PHP Function?

You can use rename_function which is also in the APD extension so you should already have it if you have override_function installed:

rename_function('call_user_func_array', 'old_user_func_array');

function call_user_func_array($method, $params) {
$params = is_array($params) ? $params : array($params);
old_call_user_func_array($method, $params);
}

Can I wrap functions in PHP?

Frame challenge: if you did succeed in this, it would be using a blacklist to achieve security, which is basically impossible to do effectively. For every function you replace with a "safe" version, there will be ten you hadn't thought of that can be used in a malicious way.

Instead, you should either use a whitelist or a sandbox.

In a whitelist approach, you don't let the user enter normal PHP at all, but a special set of functionality that you've carefully picked to allow them to do what they need. That could be an actual subset of PHP that you parse with something like nikic/php-parser, a templating language like Twig, or a completely new language you write a simple parser for.

In a sandbox approach, you allow the user to enter full PHP, but you run it in an isolated environment where they can't affect your real server. Any access to the file system or network would only be accessing virtual resources, and if the process abuses CPU or memory resources, the entire sandbox can be terminated. See for instance how the 3v4l.org site is hosted.



Related Topics



Leave a reply



Submit