登录

这一小节里面的示例敲出来报错啊

用use导入命名空间的时候 会显示语法错误,或者显示命名空间中的类已经定义了啊

的php版本是7.2.10

# PHP
宝怡 宝怡 1732 天前 1178 次浏览

全部回复(5) 我要回复

  • 王先生

    王先生2019-10-18 17:35:54

    我调整了一下调用代码的位置就可以了。哪个地方需要调用,就写在他的前面。(老师的正常显示,我们的不能正常显示其实我也搞不清楚问题在哪里。以后在编程的过程中注意不要重名就好了或者用其他方式规避重名,不一定要完完全全按照老师的来)

    <?php

    namespace Demo2;

    // use function Demo1\test as testAA;

    function test($a,$b)

    {

      return $a+$b;

    }

    echo test(4,5) ;// 非限定命名空间

    require('test1.php');

    use const \demo1\CITY;

    use const \demo1\COUNTRY;

    use function \Demo1\test;

    echo "<hr>";

    echo test(4,5);

    echo "<hr>";

    echo '调用test1中的常量:'.COUNTRY.'---';

    echo CITY;


     ?>


    回复
    0
  • 王先生

    王先生2019-10-18 16:52:52

    和老师一样的代码,报错:

    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代码:

    <?php 

    namespace Demo1;

    function test($a,$b)

    {

      return $a*$b;

    }

    ?>

    test2代码:

    <?php

    namespace Demo2;

    require('test1.php');

    use function Demo1\test;

    function test($a,$b)

    {

      return $a+$b;

    }

    echo test(4,5) ;// 非限定命名空间

    echo "<hr>";

    echo test(4,5);

     ?>


    回复
    0
  • 天蓬老师

    天蓬老师2019-07-25 14:06:02

    把源代码,还有出错提示全部贴上来看一下

    回复
    0
  • 宝怡

    test1.php代码: name; } } test2.php代码: name; } } echo (new namespace\test1)->getName(); echo '


    '; 报错提示: Fatal error: Cannot declare class Demo2\test1 because the name is already in use in D:\phpstudy\PHPTutorial\WWW\dododo\php\phpCh\chapter07\test2.php on line 6

    宝怡 · 2019-08-07 22:13:31
    宝怡

    test2.php最后还有一行: echo (new test1)->getName();

    宝怡 · 2019-08-07 22:14:56
  • 取消 回复 发送