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.