search

Home  >  Q&A  >  body text

The examples in this section report an error when typed.

When using use to import a namespace, a syntax error will be displayed, or it will show that the class in the namespace has been defined.

The PHP version is 7.2.10

宝怡宝怡1960 days ago1341

reply all(5)I'll reply

  • 王先生

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

    I just adjusted the position of the calling code and it worked. Wherever you need to call it, write it in front of it. (The teacher’s display is normal, but ours cannot be displayed normally. In fact, I can’t figure out what the problem is. In the future, just be careful not to duplicate names in the programming process or use other methods to avoid duplicate names. You don’t have to follow the teacher’s instructions exactly. (coming)

    <?php

    namespace Demo2;

    // use function Demo1\test as testAA;

    function test($a, $b)

    {

    return $a $b;

    }

    echo test(4,5) ;//Unqualified namespace

    require('test1.php');

    use const \demo1\CITY;

    use const \demo1\COUNTRY;

    use function \Demo1 \test;

    echo "<hr>";

    echo test(4,5);

    echo "<hr>";

    echo 'Call the constant in test1: '.COUNTRY.'---';

    echo CITY;


    ## ?>


    reply
    0
  • 王先生

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

    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
    0
  • 天蓬老师

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

    Post the source code and error messages to take a look

    reply
    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
    宝怡

    There is another line at the end of test2.php: echo (new test1)->getName();

    宝怡 · 2019-08-07 22:14:56
  • Cancelreply