A question about the PHP namespace

Source: Internet
Author: User
When programming a namespace problem, first run the code: {code ...} I tried to dynamically instantiate the class of another namespace through the variable in the getClass method, but the "Classnotfound" error was triggered. I think the $ class variable problem occurs, so I want to test... when programming such a namespace problem, first go to the code:

namespace Test {    class Test {}}namespace Chou {    use Test\Test;    class Mee {        public $class = 'Test';        public function getClass()        {            return new $this->class();        }    }    $tmp = new Mee();    $tmp->getClass();}

I tried to dynamically instantiate the Class of another namespace through the variable in the getClass method, but the "Class not found" error was triggered. I think the $ class variable is a problem, so I simplified the test code:

namespace Chou {    use Test\Test;    $test = 'Test';    $tmp = new $test();}

The error is as expected. I know that it can be solved in a fully qualified way, but I still want to understand what the principle is and hope to solve it.

I neglected myself. I found namespaces and dynamic language features in the manual..

Reply content:

When programming, we encounter such a namespace issue. First, we should go to the code:

namespace Test {    class Test {}}namespace Chou {    use Test\Test;    class Mee {        public $class = 'Test';        public function getClass()        {            return new $this->class();        }    }    $tmp = new Mee();    $tmp->getClass();}

I tried to dynamically instantiate the Class of another namespace through the variable in the getClass method, but the "Class not found" error was triggered. I think the $ class variable is a problem, so I simplified the test code:

namespace Chou {    use Test\Test;    $test = 'Test';    $tmp = new $test();}

The error is as expected. I know that it can be solved in a fully qualified way, but I still want to understand what the principle is and hope to solve it.

I neglected myself. I found namespaces and dynamic language features in the manual..

The manual is written, and namespace is used in variables.Must use a fully qualified name.

Http://php.net/manual/zh/language.nam...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.