Fatal Error:cannot Redeclare

Source: Internet
Author: User
Tags php language
In a PHP interactive environment, if you already have such a definition:

function a(){     return 'a';}

Now I find the original is wrong, need to change

 function a(){     return 'b'; }

This time will be error, Fatal Error:cannot redeclare

If you define

$b=1;

Modify it once again

$b=2;

PHP will never complain that B has been defined and cannot be redeclare again.
Excuse me, the function design can not be redeclare, what is the reason?
I read the answer, this question is actually very common, it will bring debugging trouble, I do not know how you in the real project environment is how to solve.

Qing let us consider the project scenario, you write a function to complete a function, generally do not write this function perfect once, you need to test it.
We import it, require (' myfun.php '), after using, found to modify, you open myfun.php, make changes, save the changes after you close the last PHP-A environment, re-php-a, and then import require (' myfun.php ' )。
If you do not close (or exit) the PHP-A environment, re-import the modified myfun.php, will immediately error: Fatal error:cannot redeclare.

This feature of PHP language causes debugging trouble, do you have any quick way?

Reply content:

In a PHP interactive environment, if you already have such a definition:

function a(){     return 'a';}

Now I find the original is wrong, need to change

 function a(){     return 'b'; }

This time will be error, Fatal Error:cannot redeclare

If you define

$b=1;

Modify it once again

$b=2;

PHP will never complain that B has been defined and cannot be redeclare again.
Excuse me, the function design can not be redeclare, what is the reason?
I read the answer, this question is actually very common, it will bring debugging trouble, I do not know how you in the real project environment is how to solve.

Qing let us consider the project scenario, you write a function to complete a function, generally do not write this function perfect once, you need to test it.
We import it, require (' myfun.php '), after using, found to modify, you open myfun.php, make changes, save the changes after you close the last PHP-A environment, re-php-a, and then import require (' myfun.php ' )。
If you do not close (or exit) the PHP-A environment, re-import the modified myfun.php, will immediately error: Fatal error:cannot redeclare.

This feature of PHP language causes debugging trouble, do you have any quick way?

Can you change the format first?

For weakly typed languages, PHP function overloading is not as normal as OOP.
Because a function overload must meet two conditions:
1, the number of function parameters is different.
2. The type of the parameter is different.
These two points, PHP has no way to meet, you can add more parameters to the function, but the equivalent of more than a temporary variable. Weak types are inherently indistinguishable, so they cannot be implemented.

Http://blog.csdn.net/smartyid ...

Functions cannot be defined repeatedly-this is not exactly the same as JS. JS inside the function is actually a variable, so you can re-assign value. But in PHP, the function is not the equivalent of a variable, but a symbol or a constant. It is probably because duplicate definitions can be misleading so that they are not duplicated.

... This is a procedural code without overloading the concept of it.
The first method adds
if (! function_exists (' a ')) {

function a(){     return 'a';}

}
Then the introduction of the second method was written before the first one, which seemed to be the only way

Thank you for your invitation.
I do not have a more convenient way, mainly because few people use interactive test it? Exit PHP-A Environment re-enter without hassle ...
Very simple test simulation run a bit, like you can use https://glot.io/

This really does not need to tangle, and also do not have to go from this level to tangle language whether to give overloaded function

Are you particularly fond of php-a this shell environment or what?

This problem solution in fact a lot of, the simplest, such as you think this environment debugging inconvenient, then edit a x.php file, and then PHP x.php once you modify the code results output, also played a debugging role.

If you really want to use a higher-level debug method, you can also try Xdebug

First of all, most of the languages are not supported by the redefinition of the method, PHP is naturally no exception.

Second, your debugging method is really strange, the first time to see people with php-a to debug, PHP has a variety of debugging tools, but also the unit test, you use the php-a is not considered debugging.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.