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;classComment {}//Create a BBS space (I have plans to open a forum)namespace BBS;//Import a namespace Useblog\article;//you can invoke an element with a qualified name after importing the namespace$article _comment=Newarticle\comment ();//use aliases for namespaces UseBlog\article asArte;//use aliases instead of space names$article _comment=Newarte\comment ();//Import a class Useblog\article\comment;//you can invoke an element with an unqualified name after importing a class$article _comment=NewComment ();//using aliases for classes UseBlog\article\comment asCOMT;//use aliases instead of space names$article _comment=NewCOMT ();
1. First introduction (provided the automatic loading mechanism is available)
Use // introducing class ' Oss\ossclient '
When used,
$ossClient New Oss\ossclient ($accessKeyId$accessKeySecret$endpointfalse);
Or so
$ossClient New Ossclient ($accessKeyId$accessKeySecret$endpointfalse);
All can!
2. The second method of introduction (provided that the automatic loading mechanism is available)
// the loading mechanism in thinkphp
When used, only
$ossClient New Oss\ossclient ($accessKeyId$accessKeySecret$endpointfalse); // 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<?PHPclassPrintIt {functionDoprint () {Echo' Hello World '; }}?>Index.PHP<?function__autoload ($class ) { $file=$class. '. class.php '; if(Is_file($file) ) { require_once($file); }} $obj=Newprintit ();$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,
<?classTest { Public Static functionLoadprint ($class ) { $file=$class. '. class.php '; if(Is_file($file)) { require_once($file); }}} spl_autoload_register (Array(' Test ', ' Loadprint ') );//Another way: Spl_autoload_register ("Test::loadprint"); $obj=Newprintit ();$obj-doprint ();?>
Original www.cnblogs.com/jiqing9006/p/5406994.html
Understanding of use, namespaces, Introduction class files, auto-loading classes in PHP