search

Home  >  Q&A  >  body text

Regarding the namespace problem, can any veteran please help me analyze it? I’m a noob.

//namespace1.php
<?php
namespace Demo1;
class test1
{
	private $name='www.php.cn';
	public function getName()
	{
		return $this->name;
	}
}

//namespace2.php
<?php
namespace Demo2;
require('namespace1.php');
use Demo1\test1;
class test1
{
	private $name='PHP中文网';
	public function getName()
	{
		return $this->name;
	}
}

The problem is why the following error occurs when I access namespace2.php

Fatal error: Cannot declare class Demo2\test1 because the name is already in use in

Do I have to give the class an alias when using Demo1\test1? ? 'as D1test1'????

Please guide me

小白一直白小白一直白1604 days ago1027

reply all(2)I'll reply

  • 零时差

    零时差2020-07-08 23:11:28

    The test1 class already exists in demo1. Then you applied the file require('namespace1.php'); and then referenced the demo1 class use Demo1\test1;

    Do you think there will be no error? Can you give him an alias?

    reply
    1
  • 小白一直白

    This is what I'm confused about. I mainly watched peter_zhu's video and it succeeded without setting an alias. I was confused for several days.

    小白一直白 · 2020-07-10 09:05:20
  • Cancelreply