报错 Fatal error: Cannot declare function Demo2\test because the name is already in use in
王先生
王先生 2019-10-18 16:53:44
0
13
1566

The same code as the teacher, error:

Fatal error: Cannot declare function Demo2\test because the name is already in use in E:\wwwroot\phpbase\object\namespace\test2.php on line 5

test1 code:

<?php

namespace Demo1;

function test($a,$b)

{

return $a*$b;

}

?>

test2 code:

<?php

namespace Demo2;

require('test1.php');

use function Demo1\test;

function test($a,$b)

{

return $a $b%

王先生
王先生

reply all(10)
王先生

You are very studious, big brother!

老老宋

I do not know

老老宋

Screenshot_6.pngThe final result is 9

老老宋

ssss.pngtest2.php file

老老宋

666666.pngtest1.php file

  • reply In fact, it can also be solved by adjusting the position of the call (put it in front of the call, don't put all the introductions together). There are other solutions. I'm just curious about the same operation and the same code. The teacher's solution works, but mine doesn't.
    王先生 author 2019-10-18 18:53:21
老老宋

test1.php and test2.php are placed in different Demo1 and Demo2 folders

  • reply I put them in two files
    王先生 author 2019-10-18 18:50:01
老老宋

use function Demo1\test; Just remove this line

  • reply What is needed is to import the function through the namespace. What is the point of removing it?
    王先生 author 2019-10-18 18:30:33
王先生

The teacher's code is to solve this kind of duplication of names. His demonstration is not wrong, but if we use the same code, we will be wrong.

老老宋

test2 itself has a test method, and then the test1 you introduced also has a test method

If the same method exists in the same .php, you will get the error above

http://php.wyscdz .com

王先生

The above error is that the PHP version is 7.0.12

If you switch back to 5.6.27, the following error will be reported:

Fatal error: Call to undefined function Demo1\test()

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template