If you are using PHP 5.3 or newer you can do the following
$func = function(){ echo "Foo"; };
$func(); //to execute
$func = function(){ echo "Bar"; };
$func(); // will echo Bar
Question
When using the PHP CLI interpreter, how might one redefine a function that has a bug. For instance, the original function declaration here lacks a newline character and I would like to fix it:
$ php -a
Interactive shell
php > function drinkBeer(){
php { echo "Carlsburg";
php { }
php >
php > drinkBeer();
Carlsburgphp >
php >
php > function drinkBeer(){
php { echo "Carlsburg" . PHP_EOL;
php { }
PHP Fatal error: Cannot redeclare drinkBeer() (previously declared in php shell code:2) in php shell code on line 3
php >
We have seen similar questions asking how to redefine a function when running a PHP script however none of the similar questions address the issue of redefining functions on the PHP CLI interpreter.
For instance, the Python interpreter allows this:
$ python3
>>> def drinkBeer():
... print('Carlsburg')
...
>>> drinkBeer()
Carlsburg
>>>
>>> def drinkBeer():
... print("You've had enough!")
...
>>> drinkBeer()
You've had enough!
>>>
No correct solution
OTHER TIPS
If you are using PHP 5.3 or newer you can do the following
$func = function(){ echo "Foo"; };
$func(); //to execute
$func = function(){ echo "Bar"; };
$func(); // will echo Bar
See this for function oriented programming http://www.php.net/manual/en/function.override-function.php
Form object oriented, just declare function in child class and use it!
Hope it was helpfull...
Good luck!