use在命名空间中的作用,如何实现自动加载带有命名空间的类?
//0817s.php <?php //use在命名空间中的作用,如何实现自动加载带有命名空间的类? //首先写一个自动加载的php文件,实际业务实现代码只需要引入这个文件 当调取某些不存在的类 就会根据自动加载php文件加载逻辑 文件位置 自定义分隔符 // 拼接目标类的域名空间路径 spl_autoload_register(function ($className){ // 字符串拼接路径 $file = str_replace('\\',DIRECTORY_SEPARATOR,$className).'.php'; if(!is_file($file) && file_exists($file)){ throw new \Exception('文件名错误或者文件不存在'); } require $file; });
//t.php <?php require '0817s.php'; use test\A as Aclass; $obj = new Aclass(); $obj->index();
//A.php A类 test/A <?php namespace test; //use 简化引入文件 其中as更能简化缩写成想要的类名称 方便调用 use test\GoodsCate as Cate; class A { public function index(){ echo '1231'; } }
//mysql ddl实操 create drop databse; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aaz | | admin-tp6 | | cmf | | easyadmin | | heima | | hm | | lycms | | mysql | | newtp | | nonecms | | performance_schema | | pyg | | snake | | sys | | thinkcmf | | xhhds | | yzncms | +--------------------+ 18 rows in set (0.01 sec) mysql> create database wtes; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aaz | | admin-tp6 | | cmf | | easyadmin | | heima | | hm | | lycms | | mysql | | newtp | | nonecms | | performance_schema | | pyg | | snake | | sys | | thinkcmf | | wtes | | xhhds | | yzncms | +--------------------+ 19 rows in set (0.00 sec) mysql> drop database wtes; Query OK, 0 rows affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | aaz | | admin-tp6 | | cmf | | easyadmin | | heima | | hm | | lycms | | mysql | | newtp | | nonecms | | performance_schema | | pyg | | snake | | sys | | thinkcmf | | xhhds | | yzncms | +--------------------+ 18 rows in set (0.00 sec) mysql> use lycms Database changed mysql> show tables; +-----------------+ | Tables_in_lycms | +-----------------+ | v1_ad | | v1_banner | | v1_collect | | v1_column | | v1_email_log | | v1_kami | | v1_say | | v1_see_record | | v1_tort | | v1_user | | v1_user_info | | v1_vod | | v1_vod_actor | | v1_vod_director | | v1_vod_play | | v1_vod_type | +-----------------+ 16 rows in set (0.00 sec) mysql> create table v1_test(id int(10),name varchar(8),sex tinyint(1)); Query OK, 0 rows affected (0.01 sec) mysql> show tables; +-----------------+ | Tables_in_lycms | +-----------------+ | v1_ad | | v1_banner | | v1_collect | | v1_column | | v1_email_log | | v1_kami | | v1_say | | v1_see_record | | v1_test | | v1_tort | | v1_user | | v1_user_info | | v1_vod | | v1_vod_actor | | v1_vod_director | | v1_vod_play | | v1_vod_type | +-----------------+ 17 rows in set (0.00 sec) mysql> desc v1_test -> ; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | name | varchar(8) | YES | | NULL | | | sex | tinyint(1) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.01 sec) mysql> show create table v1_test; +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+ | v1_test | CREATE TABLE `v1_test` ( `id` int(10) DEFAULT NULL, `name` varchar(8) DEFAULT NULL, `sex` tinyint(1) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> alter table v1_test modify name varchar(12); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table v1_test -> ; +---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+ | v1_test | CREATE TABLE `v1_test` ( `id` int(10) DEFAULT NULL, `name` varchar(12) DEFAULT NULL, `sex` tinyint(1) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> alter table v1_test add age tinyint; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table v1_test; +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | v1_test | CREATE TABLE `v1_test` ( `id` int(10) DEFAULT NULL, `name` varchar(12) DEFAULT NULL, `sex` tinyint(1) DEFAULT NULL, `age` tinyint(4) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> alter table v1_test name uname varchar(12); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'name uname varchar(12)' at line 1 mysql> alter table v1_test modify name uname varchar(12); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'uname varchar(12)' at line 1 mysql> alter table v1_test change name uname varchar(12); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table v1_test drop age; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table v1_test; +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | v1_test | CREATE TABLE `v1_test` ( `id` int(10) DEFAULT NULL, `uname` varchar(12) DEFAULT NULL, `sex` tinyint(1) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> alter table v1_test add age tinyint before sex; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'before sex' at line 1 mysql> alter table v1_test add age tinyint after uname; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table v1_test uid int(11) first; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'uid int(11) first' at line 1 mysql> alter table v1_test add uid int(11) first; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table v1_test; +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | v1_test | CREATE TABLE `v1_test` ( `uid` int(11) DEFAULT NULL, `id` int(10) DEFAULT NULL, `uname` varchar(12) DEFAULT NULL, `age` tinyint(4) DEFAULT NULL, `sex` tinyint(1) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> alter table v1_test rename test; Query OK, 0 rows affected (0.00 sec) mysql> show create table est; ERROR 1146 (42S02): Table 'lycms.est' doesn't exist mysql> show create table test; +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | test | CREATE TABLE `test` ( `uid` int(11) DEFAULT NULL, `id` int(10) DEFAULT NULL, `uname` varchar(12) DEFAULT NULL, `age` tinyint(4) DEFAULT NULL, `sex` tinyint(1) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> create index id_index on test(id); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> create unique index uid_o on test(uid); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table test; +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | test | CREATE TABLE `test` ( `uid` int(11) DEFAULT NULL, `id` int(10) DEFAULT NULL, `uname` varchar(12) DEFAULT NULL, `age` tinyint(4) DEFAULT NULL, `sex` tinyint(1) DEFAULT NULL, UNIQUE KEY `uid_o` (`uid`), KEY `id_index` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> drop index id_index on test; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table test; +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | test | CREATE TABLE `test` ( `uid` int(11) DEFAULT NULL, `id` int(10) DEFAULT NULL, `uname` varchar(12) DEFAULT NULL, `age` tinyint(4) DEFAULT NULL, `sex` tinyint(1) DEFAULT NULL, UNIQUE KEY `uid_o` (`uid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> show index from test; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | test | 0 | uid_o | 1 | uid | A | NULL | NULL | NULL | YES | BTREE | | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 1 row in set (0.00 sec) mysql> show index from test \G; *************************** 1. row *************************** Table: test Non_unique: 0 Key_name: uid_o Seq_in_index: 1 Column_name: uid Collation: A Cardinality: NULL Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 1 row in set (0.00 sec) ERROR: No query specified mysql>
//mysqlDML实操 mysql> insert into test (uid,id,uname,age,sex) values (1,1,'yyds',11,1); Query OK, 1 row affected (0.01 sec) mysql> select * from test; +------+------+-------+------+------+ | uid | id | uname | age | sex | +------+------+-------+------+------+ | 1 | 1 | yyds | 11 | 1 | +------+------+-------+------+------+ 1 row in set (0.00 sec) mysql> insert into test (uid,id,uname,age,sex) values (2,2,'yyds',11,1); Query OK, 1 row affected (0.00 sec) mysql> select * from test; +------+------+-------+------+------+ | uid | id | uname | age | sex | +------+------+-------+------+------+ | 1 | 1 | yyds | 11 | 1 | | 2 | 2 | yyds | 11 | 1 | +------+------+-------+------+------+ 2 rows in set (0.00 sec) mysql> delete from test where uid = 2; Query OK, 1 row affected (0.00 sec) mysql> select * from test; +------+------+-------+------+------+ | uid | id | uname | age | sex | +------+------+-------+------+------+ | 1 | 1 | yyds | 11 | 1 | +------+------+-------+------+------+ 1 row in set (0.00 sec) mysql> update test set uname = 'xswl' where id = 1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from test; +------+------+-------+------+------+ | uid | id | uname | age | sex | +------+------+-------+------+------+ | 1 | 1 | xswl | 11 | 1 | +------+------+-------+------+------+ 1 row in set (0.00 sec) mysql>