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
王先生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;
王先生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%