81

Can you declare a function like this...

function ihatefooexamples(){
  return "boo-foo!";
};

And then redeclare it somewhat like this...

if ($_GET['foolevel'] == 10){
  function ihatefooexamples(){
    return "really boo-foo";
  };
};

Is it possible to overwrite a function that way?

Any way?

3

10 Answers 10

110

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();
12
  • 23
    +1 for solving the actual problem, rather than simply trying to answer the exact question...
    – ircmaxell
    Sep 1, 2010 at 17:52
  • 2
    The only change I would make would to put the $foo = new Foo() in an else block, in case either the constructor (a) is expensive, or (b) has side-effects. Sep 1, 2010 at 18:02
  • 2
    @Gordon You're over-analyzing. I think it's pretty clear that this is just an example. Sep 1, 2010 at 20:34
  • 11
    I disagree, with this answer, the question clearly says "function", not "method", it is clear that the context is not a Class. I came here from a Google search, at the moment I'm using a non-OO legacy library and this answer is just useless.
    – mastazi
    Aug 18, 2015 at 3:36
  • 5
    @mastazi Thank you for pointing out to me that this is a top answer given by google searches on this question. I have updated the answer with information that should help future answer seekers with similar goals to your own. Oct 11, 2016 at 14:17
87

Monkey patch in namespace php >= 5.3

A less evasive method than modifying the interpreter is the monkey patch.

Monkey patching is the art of replacing the actual implementation with a similar "patch" of your own.

Ninja skills

Before you can monkey patch like a PHP Ninja we first have to understand PHPs namespaces.

Since PHP 5.3 we got introduced to namespaces which you might at first glance denote to be equivalent to something like java packages perhaps, but it's not quite the same. Namespaces, in PHP, is a way to encapsulate scope by creating a hierarchy of focus, especially for functions and constants. As this topic, fallback to global functions, aims to explain.

If you don't provide a namespace when calling a function, PHP first looks in the current namespace then moves down the hierarchy until it finds the first function declared within that prefixed namespace and executes that. For our example if you are calling print_r(); from namespace My\Awesome\Namespace; What PHP does is to first look for a function called My\Awesome\Namespace\print_r(); then My\Awesome\print_r(); then My\print_r(); until it finds the PHP built in function in the global namespace \print_r();.

You will not be able to define a function print_r($object) {} in the global namespace because this will cause a name collision since a function with that name already exists.

Expect a fatal error to the likes of:

Fatal error: Cannot redeclare print_r()

But nothing stops you, however, from doing just that within the scope of a namespace.

Patching the monkey

Say you have a script using several print_r(); calls.

Example:

<?php
     print_r($some_object);
     // do some stuff
     print_r($another_object);
     // do some other stuff
     print_r($data_object);
     // do more stuff
     print_r($debug_object);

But you later change your mind and you want the output wrapped in <pre></pre> tags instead. Ever happened to you?

Before you go and change every call to print_r(); consider monkey patching instead.

Example:

<?php
    namespace MyNamespace {
        function print_r($object) 
        {
            echo "<pre>", \print_r($object, true), "</pre>"; 
        }

        print_r($some_object);
        // do some stuff
        print_r($another_object);
        // do some other stuff
        print_r($data_object);
        // do more stuff
        print_r($debug_object);
    }

Your script will now be using MyNamespace\print_r(); instead of the global \print_r();

Works great for mocking unit tests.

nJoy!

6
  • 4
    A really valuable approach! Thanks for sharing
    – Nico Haase
    Mar 21, 2016 at 14:54
  • 1
    This is an incredibly helpful answer for those who are stuck with legacy, non-oo libraries!
    – mastazi
    Oct 11, 2016 at 21:32
  • 2
    I can't make this work when functions A, B, and C are defined in an existing large include file (I don't want to change these, because other pages use them) and I want to override just function B in the main file, where B is defined in a separate include file. I guess namespaces don't work in this case. Oct 22, 2018 at 14:03
  • @DavidSpector you need to namespace where it is redeclared which you want to do in a separate include file ex. namespace SeparateInclude {} then in the main file you can namespace again namespace MainFile {}, include the new file create monkey patch function B() again in main file which calls \SeparateInclude\B();. When you call B(); in main file now it calls \MainFile\B(); which calls \SeparateInclude\B(); successfully avoiding \B(); from the large/original include file.
    – nickl-
    Jun 5, 2019 at 5:18
  • 1
    Does it actually move down the namespaces because it doesn't seem to? It seems A\B\C trying to use an "f" will check for \A\B\C\f or \f ("top level f") only, The \ are LITERALLY names with the \ at the start only having special meaning (like concatenating or not the current namespace as a prefix, or not doing this) - can you confirm? Because it'd make sense if it did! But it's PHP so they wont do that (I've tried and read extensively, this gave me a glimmer of hope - crush or or show me what I missed please! PHP5 would be great for the answer BTW)
    – Alec Teal
    Sep 7, 2019 at 19:54
25

Have a look at override_function to override the functions.

override_function — Overrides built-in functions

Example:

override_function('test', '$a,$b', 'echo "DOING TEST"; return $a * $b;');
10
  • 2
    this is a good point but if the function is large, its not nice to pass in strings that long! :(, another point is that this can only be used for built in functions and not user defined!
    – RobertPitt
    Sep 1, 2010 at 17:39
  • @RobertPitt: Agreed this is what we have at the moment and your solution looks good but that depends on php version being used.
    – Sarfraz
    Sep 1, 2010 at 17:41
  • Why would you not want to be on a php version < 5.3.0? unless your building a script that would be retailed/released to the public then you have a valid point.
    – RobertPitt
    Sep 1, 2010 at 17:44
  • @RobertPitt: I am using 5.3 and hope OP is using the same too :)
    – Sarfraz
    Sep 1, 2010 at 17:46
  • 2
    I tried using override function but its a part of PECL and PECL needs to be configured first.
    – user379888
    Dec 28, 2017 at 10:19
14

short answer is no, you can't overwrite a function once its in the PHP function scope.

your best of using anonymous functions like so

$ihatefooexamples = function()
{
  return "boo-foo!";
}

//...
unset($ihatefooexamples);
$ihatefooexamples = function()
{
   return "really boo-foo";
}

http://php.net/manual/en/functions.anonymous.php

5
  • Do you really need the unset? I know it isn't bad to do, just curious (I haven't tried this specific case)...
    – ircmaxell
    Sep 1, 2010 at 17:55
  • 4
    @ircmax You don't have to use unset Sep 1, 2010 at 18:01
  • I haven't a clue on anonymous functions I just know they how to create them but iv'e never played with them, i understand that unset can be costly if overused but depending on the size of the function it might be better !
    – RobertPitt
    Sep 1, 2010 at 18:35
  • 1
    I don't know why this didn't get the merit it required. It is the cleanest, usable and sensible approach. The OP didn't ask for an OOP approach (quite the contrary in fact), and the 3rd-party functionality simply does not work, hence this is the only solution.
    – Christian
    Sep 5, 2011 at 1:09
  • This is not overriding any function it's merely changing the value of a variable. you would've had the same result leaving the closure (anonymous function) part out completely. This does not answer the question!
    – nickl-
    Aug 26, 2012 at 6:17
11

You cannot redeclare any functions in PHP. You can, however, override them. Check out overriding functions as well as renaming functions in order to save the function you're overriding if you want.

So, keep in mind that when you override a function, you lose it. You may want to consider keeping it, but in a different name. Just saying.

Also, if these are functions in classes that you're wanting to override, you would just need to create a subclass and redeclare the function in your class without having to do rename_function and override_function.

Example:

rename_function('mysql_connect', 'original_mysql_connect' );
override_function('mysql_connect', '$a,$b', 'echo "DOING MY FUNCTION INSTEAD"; return $a * $b;');
1
5

I would include all functions of one case in an include file, and the others in another include.

For instance simple.inc would contain function boofoo() { simple } and really.inc would contain function boofoo() { really }

It helps the readability / maintenance of your program, having all functions of the same kind in the same inc.

Then at the top of your main module

  if ($_GET['foolevel'] == 10) {
    include "really.inc";
  }
  else {
    include "simple.inc";
  }
2
  • If it is required (doesn't seem so) one can add a $iam = "really" or $iam = "simple" at the top of the include files.
    – Déjà vu
    Sep 5, 2011 at 15:45
  • 1
    I think this is the best non OOP answer on this question as it shows a procedural implementation of the State Pattern. (Although I would prefer an implementation where the ifs are replaced by a foreach loop.) I have used this pattern successfully. You can figure out which function is used by logging. The log messages are of a level that is not used anymore in production or acceptance environments. Jan 24, 2016 at 8:23
4

You could use the PECL extension

but that is bad practise in my opinion. You are using functions, but check out the Decorator design pattern. Can borrow the basic idea from it.

1

No this will be a problem. PHP Variable Functions

1

Depending on situation where you need this, maybe you can use anonymous functions like this:

$greet = function($name)
{
    echo('Hello ' . $name);
};

$greet('World');

...then you can set new function to the given variable any time

1

A solution for the related case where you have an include file A that you can edit and want to override some of its functions in an include file B (or the main file):

Main File:

<?php
$Override=true; // An argument used in A.php
include ("A.php");
include ("B.php");
F1();
?>

Include File A:

<?php
if (!@$Override) {
   function F1 () {echo "This is F1() in A";}
}
?>

Include File B:

<?php
   function F1 () {echo "This is F1() in B";}
?>

Browsing to the main file displays "This is F1() in B".

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.