Perfect solution to the namespace problem in the ThinkPHP controller, thinkphp namespace

Source: Internet
Author: User

Perfect solution to the namespace problem in the ThinkPHP controller, thinkphp namespace

In TP a few days ago, I used PHP to bring my own class, because of the namespace, I encountered a small problem. Because the namespace is used in ThinkPHP, if it is in the Home namespace, directly execute the following code

<Pre name = "code" class = "php"> $ datetime = new DateTime ($ basedate, new DateTimeZone ($ timezone ));

Then it is equivalent

$ Datetime = new \ Home \ DateTime ($ basedate, new <span style = "font-family: Arial, Helvetica, sans-serif; ">\home \ </span> <span style =" font-family: Arial, Helvetica, sans-serif; "> DateTimeZone ($ timezone); </span>

It is displayed that this is incorrect DateTime and DateTimeZone are built-in class files in PHP and should be in the Root Space. The correct execution method is as follows:

$ Datetime = new \ DateTime ($ basedate, new \ DateTimeZone ($ timezone ));

Note:If you do not specify a namespace when instantiating a class, PHP uses the current namespace. If the current namespace is not specified, the current namespace is the root space, that is '\

The above is a perfect solution to the namespace problem in the ThinkPHP controller. It is all the content shared by the editor. I hope to give you a reference and support for the help house.

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.