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

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)

<?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;


## ?>


王先生

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%

Peter-Zhu

Post the source code and error messages to take a look

  • reply test1.php代码: <?php namespace Demo1; class test1 { private $name = 'www.php.cn'; public function getName() { return $this->name; } } test2.php代码: <?php namespace Demo2; require('test1.php'); // use 默认从全局开始引入 use Demo1\test1; class test1 { private $name = 'php中文网'; public function getName() { return $this->name; } } echo (new namespace\test1)->getName(); echo '<hr/>'; 报错提示: 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