补0817:域名空间内类实现自动加载,use的作用 及 mysql数据库DDL,DML语句实操

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)){thrownew\Exception('文件名错误或者文件不存在');}require$file;});
//t.php<?phprequire'0817s.php';usetest\AasAclass;$obj=newAclass();$obj->index();
//A.phpA类test/A<?phpnamespacetest;//use简化引入文件其中as更能简化缩写成想要的类名称方便调用usetest\GoodsCateasCate;classA{publicfunctionindex(){echo'1231';}}
//mysqlddl实操createdropdatabse;mysql>showdatabases;+--------------------+|Database|+--------------------+|information_schema||aaz||admin-tp6||cmf||easyadmin||heima||hm||lycms||mysql||newtp||nonecms||performance_schema||pyg||snake||sys||thinkcmf||xhhds||yzncms|+--------------------+18rowsinset(0.01sec)mysql>createdatabasewtes;QueryOK,1rowaffected(0.00sec)mysql>showdatabases;+--------------------+|Database|+--------------------+|information_schema||aaz||admin-tp6||cmf||easyadmin||heima||hm||lycms||mysql||newtp||nonecms||performance_schema||pyg||snake||sys||thinkcmf||wtes||xhhds||yzncms|+--------------------+19rowsinset(0.00sec)mysql>dropdatabasewtes;QueryOK,0rowsaffected(0.01sec)mysql>showdatabases;+--------------------+|Database|+--------------------+|information_schema||aaz||admin-tp6||cmf||easyadmin||heima||hm||lycms||mysql||newtp||nonecms||performance_schema||pyg||snake||sys||thinkcmf||xhhds||yzncms|+--------------------+18rowsinset(0.00sec)mysql>uselycmsDatabasechangedmysql>showtables;+-----------------+|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|+-----------------+16rowsinset(0.00sec)mysql>createtablev1_test(idint(10),namevarchar(8),sextinyint(1));QueryOK,0rowsaffected(0.01sec)mysql>showtables;+-----------------+|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|+-----------------+17rowsinset(0.00sec)mysql>descv1_test->;+-------+------------+------+-----+---------+-------+|Field|Type|Null|Key|Default|Extra|+-------+------------+------+-----+---------+-------+|id|int(10)|YES||NULL|||name|varchar(8)|YES||NULL|||sex|tinyint(1)|YES||NULL||+-------+------------+------+-----+---------+-------+3rowsinset(0.01sec)mysql>showcreatetablev1_test;+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+|Table|CreateTable|+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+|v1_test|CREATETABLE`v1_test`(`id`int(10)DEFAULTNULL,`name`varchar(8)DEFAULTNULL,`sex`tinyint(1)DEFAULTNULL)ENGINE=MyISAMDEFAULTCHARSET=utf8|+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+1rowinset(0.00sec)mysql>altertablev1_testmodifynamevarchar(12);QueryOK,0rowsaffected(0.01sec)Records:0Duplicates:0Warnings:0mysql>showcreatetablev1_test->;+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+|Table|CreateTable|+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+|v1_test|CREATETABLE`v1_test`(`id`int(10)DEFAULTNULL,`name`varchar(12)DEFAULTNULL,`sex`tinyint(1)DEFAULTNULL)ENGINE=MyISAMDEFAULTCHARSET=utf8|+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+1rowinset(0.00sec)mysql>altertablev1_testaddagetinyint;QueryOK,0rowsaffected(0.01sec)Records:0Duplicates:0Warnings:0mysql>showcreatetablev1_test;+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|Table|CreateTable|+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|v1_test|CREATETABLE`v1_test`(`id`int(10)DEFAULTNULL,`name`varchar(12)DEFAULTNULL,`sex`tinyint(1)DEFAULTNULL,`age`tinyint(4)DEFAULTNULL)ENGINE=MyISAMDEFAULTCHARSET=utf8|+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1rowinset(0.00sec)mysql>altertablev1_testnameunamevarchar(12);ERROR1064(42000):YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'nameunamevarchar(12)'atline1mysql>altertablev1_testmodifynameunamevarchar(12);ERROR1064(42000):YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'unamevarchar(12)'atline1mysql>altertablev1_testchangenameunamevarchar(12);QueryOK,0rowsaffected(0.00sec)Records:0Duplicates:0Warnings:0mysql>altertablev1_testdropage;QueryOK,0rowsaffected(0.01sec)Records:0Duplicates:0Warnings:0mysql>showcreatetablev1_test;+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+|Table|CreateTable|+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+|v1_test|CREATETABLE`v1_test`(`id`int(10)DEFAULTNULL,`uname`varchar(12)DEFAULTNULL,`sex`tinyint(1)DEFAULTNULL)ENGINE=MyISAMDEFAULTCHARSET=utf8|+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+1rowinset(0.00sec)mysql>altertablev1_testaddagetinyintbeforesex;ERROR1064(42000):YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'beforesex'atline1mysql>altertablev1_testaddagetinyintafteruname;QueryOK,0rowsaffected(0.01sec)Records:0Duplicates:0Warnings:0mysql>altertablev1_testuidint(11)first;ERROR1064(42000):YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'uidint(11)first'atline1mysql>altertablev1_testadduidint(11)first;QueryOK,0rowsaffected(0.01sec)Records:0Duplicates:0Warnings:0mysql>showcreatetablev1_test;+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|Table|CreateTable|+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|v1_test|CREATETABLE`v1_test`(`uid`int(11)DEFAULTNULL,`id`int(10)DEFAULTNULL,`uname`varchar(12)DEFAULTNULL,`age`tinyint(4)DEFAULTNULL,`sex`tinyint(1)DEFAULTNULL)ENGINE=MyISAMDEFAULTCHARSET=utf8|+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1rowinset(0.00sec)mysql>altertablev1_testrenametest;QueryOK,0rowsaffected(0.00sec)mysql>showcreatetableest;ERROR1146(42S02):Table'lycms.est'doesn'texistmysql>showcreatetabletest;+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|Table|CreateTable|+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|test|CREATETABLE`test`(`uid`int(11)DEFAULTNULL,`id`int(10)DEFAULTNULL,`uname`varchar(12)DEFAULTNULL,`age`tinyint(4)DEFAULTNULL,`sex`tinyint(1)DEFAULTNULL)ENGINE=MyISAMDEFAULTCHARSET=utf8|+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1rowinset(0.00sec)mysql>createindexid_indexontest(id);QueryOK,0rowsaffected(0.01sec)Records:0Duplicates:0Warnings:0mysql>createuniqueindexuid_oontest(uid);QueryOK,0rowsaffected(0.01sec)Records:0Duplicates:0Warnings:0mysql>showcreatetabletest;+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|Table|CreateTable|+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|test|CREATETABLE`test`(`uid`int(11)DEFAULTNULL,`id`int(10)DEFAULTNULL,`uname`varchar(12)DEFAULTNULL,`age`tinyint(4)DEFAULTNULL,`sex`tinyint(1)DEFAULTNULL,UNIQUEKEY`uid_o`(`uid`),KEY`id_index`(`id`))ENGINE=MyISAMDEFAULTCHARSET=utf8|+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1rowinset(0.00sec)mysql>dropindexid_indexontest;QueryOK,0rowsaffected(0.01sec)Records:0Duplicates:0Warnings:0mysql>showcreatetabletest;+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|Table|CreateTable|+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+|test|CREATETABLE`test`(`uid`int(11)DEFAULTNULL,`id`int(10)DEFAULTNULL,`uname`varchar(12)DEFAULTNULL,`age`tinyint(4)DEFAULTNULL,`sex`tinyint(1)DEFAULTNULL,UNIQUEKEY`uid_o`(`uid`))ENGINE=MyISAMDEFAULTCHARSET=utf8|+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1rowinset(0.00sec)mysql>showindexfromtest;+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+|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|||+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+1rowinset(0.00sec)mysql>showindexfromtest\G;***************************1.row***************************Table:testNon_unique:0Key_name:uid_oSeq_in_index:1Column_name:uidCollation:ACardinality:NULLSub_part:NULLPacked:NULLNull:YESIndex_type:BTREEComment:Index_comment:1rowinset(0.00sec)ERROR:Noqueryspecifiedmysql>
//mysqlDML实操mysql>insertintotest(uid,id,uname,age,sex)values(1,1,'yyds',11,1);QueryOK,1rowaffected(0.01sec)mysql>select*fromtest;+------+------+-------+------+------+|uid|id|uname|age|sex|+------+------+-------+------+------+|1|1|yyds|11|1|+------+------+-------+------+------+1rowinset(0.00sec)mysql>insertintotest(uid,id,uname,age,sex)values(2,2,'yyds',11,1);QueryOK,1rowaffected(0.00sec)mysql>select*fromtest;+------+------+-------+------+------+|uid|id|uname|age|sex|+------+------+-------+------+------+|1|1|yyds|11|1||2|2|yyds|11|1|+------+------+-------+------+------+2rowsinset(0.00sec)mysql>deletefromtestwhereuid=2;QueryOK,1rowaffected(0.00sec)mysql>select*fromtest;+------+------+-------+------+------+|uid|id|uname|age|sex|+------+------+-------+------+------+|1|1|yyds|11|1|+------+------+-------+------+------+1rowinset(0.00sec)mysql>updatetestsetuname='xswl'whereid=1;QueryOK,1rowaffected(0.00sec)Rowsmatched:1Changed:1Warnings:0mysql>select*fromtest;+------+------+-------+------+------+|uid|id|uname|age|sex|+------+------+-------+------+------+|1|1|xswl|11|1|+------+------+-------+------+------+1rowinset(0.00sec)mysql>