Use just uses a namespace,
But to invoke the class, you have to load the class file, or load it automatically.
Even if one of the classes is introduced, if there is no automatic loading mechanism, it will be an error.
Several usages of use
Namespace Blog\article;class Comment {}//Create a BBS space (I'm going to open a forum) namespace bbs;//Import a namespace use blog\article;// The namespace can be invoked with qualified names after importing the namespaces $article_comment = new Article\comment ();//use aliases for namespaces using Blog\article as arte;//use aliases instead of space names $article_ Comment = new arte\comment ();//Import a class use blog\article\comment;//import a class to invoke an element with an unqualified name $article_comment = new comment ();// Use aliases for classes using Blog\article\comment as comt;//use aliases instead of space names $article_comment = new COMT ();
1. First introduction (provided the automatic loading mechanism is available)
Use oss\ossclient; Introducing class ' Oss\ossclient '
When used,
$ossClient = new Oss\ossclient ($accessKeyId, $accessKeySecret, $endpoint, false);
Or so
$ossClient = new Ossclient ($accessKeyId, $accessKeySecret, $endpoint, false);
All can!
2. The second method of introduction (provided that the automatic loading mechanism is available)
Import (' @.org.oss.ossclient '); The loading mechanism in thinkphp
When used, only
$ossClient = new Oss\ossclient ($accessKeyId, $accessKeySecret, $endpoint, false); Where OSS is the namespace
There is a mechanism for automatically loading namespaces in thinkphp,
The namespaces in the framework liberary directory can be automatically identified and positioned as follows
Library Framework Class Libraries Directory
│├─think core Think Class Library package directory
│├─org ORG Class Library Package directory
│├─ ... More Class Library Catalogs
So, if you have a namespace, you do not need to introduce a file.
But without namespaces, if you do not introduce a file, you will get an error.
Import it for a moment,
3.autoload
This is an auto-load function, which is triggered when we instantiate an undefined class in PHP5. Look at the following example:
printit.class.php <?php class PrintIt { function doprint () { echo ' Hello World ';}}? > index.php <?function autoload ($class) {$file = $class. '. class.php '; if (Is_file ($file)) { require_once ($file);} } $obj = new PrintIt (); $obj->doprint ();? >
Normal output Hello world after running index.php. In index.php, because the printit.class.php is not included, the AutoLoad function is automatically called when the PrintIt is instantiated, and the value of the parameter $class is the class name PrintIt, At this time, printit.class.php was introduced.
4.spl_autoload_register
Look again at Spl_autoload_register (), this function and autoload with the Kutong of the wonderful, see a simple example:
<?function Loadprint ($class) {$file = $class. '. class.php '; if (Is_file ($file)) { require_once ($file);} } Spl_autoload_register (' Loadprint '); $obj = new PrintIt (); $obj->doprint ();? >
Replace the autoload with the Loadprint function. But Loadprint does not automatically trigger like AutoLoad, when Spl_autoload_register () works, which tells PHP to execute Loadprint () when it encounters a class that is not defined.
Spl_autoload_register () calls a static method,
<? Class Test {public static function Loadprint ($class) { $file = $class. '. class.php '; if (Is_file ($file)) { require_once ($file);}} } Spl_autoload_register ( Array (' Test ', ' Loadprint ') );//Another notation: Spl_autoload_register ( "Test::loadprint " ); $obj = new PrintIt (); $obj->doprint ();? >