Why can I use external objects without using Include in the namespace of YIi2?

Source: Internet
Author: User
For example, I can see that namespaces can be directly loaded in some write methods of Yii2, such as: {code ...} in the above webmodels namespace, the CPost object can be directly created using the new keyword, but I independently tested the namespace as follows: {code ...} in the test generation... for example, in some Yii2 write methods, namespaces can be directly loaded. for example:

 namespace web\models;        use Yii;     use web\classes\CPost;       //... code ...          $post = new CPost;

In the preceding web \ models namespace, CPost objects can be directly created using the new keyword,
However, I independently tested the namespace as follows:

Namespace web \ models; // include ".. /classes/CPost. php "; this sentence must be uncommented before the use web \ classes \ CPost; class MPost {public function run () {echo" MPost-> run () is called ";} public function getClassFunc () {$ class = new CPost; $ class-> run (); // error, prompt that the web \ classes \ CPost object is not found} $ post = new MPost; $ post-> getClassFunc ();

In the test code, if the inlucde CPost file is not used, an error is reported,
However, no file is introduced throughout Yii2, and you can directly create an object using a namespace.
What is going on? how is this done?

Reply content:

For example, in some Yii2 write methods, namespaces can be directly loaded. for example:

 namespace web\models;        use Yii;     use web\classes\CPost;       //... code ...          $post = new CPost;

In the preceding web \ models namespace, CPost objects can be directly created using the new keyword,
However, I independently tested the namespace as follows:

Namespace web \ models; // include ".. /classes/CPost. php "; this sentence must be uncommented before the use web \ classes \ CPost; class MPost {public function run () {echo" MPost-> run () is called ";} public function getClassFunc () {$ class = new CPost; $ class-> run (); // error, prompt that the web \ classes \ CPost object is not found} $ post = new MPost; $ post-> getClassFunc ();

In the test code, if the inlucde CPost file is not used, an error is reported,
However, no file is introduced throughout Yii2, and you can directly create an object using a namespace.
What is going on? how is this done?

Spl autoload, learn composer by the way

Check the content of the composer. json file, which contains autoload to see if it defines whether the entire directory is automatically loaded or only a few files.

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.