search

Home  >  Q&A  >  body text

报错 Fatal error: Cannot declare function Demo2\test because the name is already in use in

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%

王先生王先生1893 days ago1596

reply all(13)I'll reply

  • 王先生

    王先生2019-10-19 09:42:11

    You are very studious, big brother!

    reply
    0
  • 老老宋

    老老宋2019-10-19 09:36:04

    I do not know

    reply
    0
  • 老老宋

    老老宋2019-10-18 18:51:17

    Screenshot_6.pngThe final result is 9

    reply
    0
  • 老老宋

    老老宋2019-10-18 18:50:36

    ssss.pngtest2.php file

    reply
    0
  • 老老宋

    老老宋2019-10-18 18:49:49

    666666.pngtest1.php file

    reply
    0
  • 王先生

    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.

    王先生 · 2019-10-18 18:53:21
  • 老老宋

    老老宋2019-10-18 18:46:24

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

    reply
    0
  • 王先生

    I put them in two files

    王先生 · 2019-10-18 18:50:01
  • 老老宋

    老老宋2019-10-18 18:27:59

    use function Demo1\test; Just remove this line

    reply
    0
  • 王先生

    What is needed is to import the function through the namespace. What is the point of removing it?

    王先生 · 2019-10-18 18:30:33
  • 王先生

    王先生2019-10-18 18:12:30

    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.

    reply
    0
  • 老老宋

    老老宋2019-10-18 18:03:47

    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

    reply
    0
  • 王先生

    王先生2019-10-18 16:59:48

    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()

    reply
    0
  • Cancelreply