The examples in this section report an error when typed.
宝怡
宝怡 2019-07-23 13:28:20
0
5
1268

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

宝怡
宝怡

reply all (3)
王先生

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)

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 "


";

echo test(4,5);

echo "


";

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

echo CITY;


## ?>


    王先生

    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:

    namespace Demo1;

    function test($a,$b)

    {

    return $a*$b;

    }

    ?>

    test2 code:

    namespace Demo2;

    require('test1.php');

    use function Demo1\test;

    function test($a,$b)

    {

    return $a $b%

      Peter-Zhu

      Post the source code and error messages to take a look

      • reply 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
        宝怡 author 2019-08-07 22:13:31
      • reply There is another line at the end of test2.php: echo (new test1)->getName();
        宝怡 author 2019-08-07 22:14:56
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!