문제

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!
>>>

올바른 솔루션이 없습니다

다른 팁

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top