Php namespace class usage questions

Source: Internet
Author: User
A class is written and namespace is used: 'namespaceab; classDemoextendsXXX {code ...}} 'Use in another file: 'useabdemo; $ dnewDemo (); 'prompt: the XXX class cannot be found. My own class uses the namespace. The inherited XXX class belongs to the... class and uses the namespace :'
Namespace a \ B;
Class Demo extends XXX {

……

}'

In another file :'
Use a \ B \ Demo;
$ D = new Demo ();
`

Tip: the XXX class cannot be found.

My own class uses the namespace. The inherited XXX class belongs to a third-party class library. The third-party class library does not use the namespace and uses the require_once file.

I don't know how to solve this problem. It's unrealistic to change all third-party class libraries to namespaces. I can't do this whenever I use a third party. The amount of work is huge.

Reply content:

A class is written and namespace is used :'
Namespace a \ B;
Class Demo extends XXX {

……

}'

In another file :'
Use a \ B \ Demo;
$ D = new Demo ();
`

Tip: the XXX class cannot be found.

My own class uses the namespace. The inherited XXX class belongs to a third-party class library. The third-party class library does not use the namespace and uses the require_once file.

I don't know how to solve this problem. It's unrealistic to change all third-party class libraries to namespaces. I can't do this whenever I use a third party. The amount of work is huge.

A. php


  ';    }}

C. php


  ';    }}

B. php


  out();

Which probably means it?

If no namespace is set for a third-party class, a top-level namespace '\' is added by default to PHP.

By the way, pay attention to the file loading sequence.

class Demo extends \XXX

1. The XXX class and the class to be used in this class must be added with namespace
2. The code must be modified:

Namespace a \ B; use path/XXX; // here, the class inherited by XXX must be imported into class Demo extends XXX {…} using the use Operator {......} '

These two conditions are indispensable !!!

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.