Override Default PHP Function

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.

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

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.

Override a default php function ? (eval)

namespace blarg;
function time() {
echo "test !";
}
time();

This does not override the builtin time() function. What this does is create the function blarg\time() -- that is, both time() and blarg\time() exist and can be called, so never was time() overridden.

When you call time() in the blarg namespace, both the builtin time() and local blarg\time() functions are candidates to be called; however, PHP will disambiguate by always calling the most local name, which in this case is blarg\time().

Now, if we ask if it is possible to introduce a function locally named eval(), the answer is no, because eval is a reserved word in the PHP language because of its special meaning -- referred to as a language construct. Other examples of the same nature are isset, empty, include, and require.

php does not let me override default parameter

You have set the default value for $isbilling to false, and you aren't passing anything to the $isbilling parameter when you call it (I'm assuming, since you didn't actually post that part).

Try this:

$isbilling = true;

function retrieve_address($dbh, $customer_id, $isbilling=false){
echo $isbilling; //false
}

retrieve_address($dbh, $customer_id, $isbilling);

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();


Related Topics



Leave a reply



Submit