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.'---';
replytest1.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
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%
Post the source code and error messages to take a look