PHP基础知识:命名空间的使用、自动加载器和数据库操作
一、命名空间的使用:
1.非限定名称:当前空间下使用,类名无空间前缀
2.限定名称:相对路径,类名无全局空间前缀
3.完全限定名称:绝对路径从根空间开始访问\
表示根空间
4.通过use
关键字来调用命名空间的类,可以通过as
关键字简化或起别名避免冲突;如果别名和类名相同时,use语句可以省略as关键字和别名;
5.案例
<?php
//命名空间引用方式
// 非限定名称
namespace one
{
class Demo{}
$str=Demo::class;//非限定名称(当前空间中成员的调用)
echo $str."<br>";
$str=two\Demo::class;//限定名称(相对当前空间相对路径)
echo $str."<br>";
}
namespace one\two
{
class Demo{}
$str = \one\Demo::class;//完全限定名称(从跟空间开始,调用其他空间的成员)
echo $str;
}
namespace {
echo "<hr>";
}
二、自动加载函数
1.自动加载函数:spl_autoload_register();
2.自动加载函数的参数:匿名函数(闭包);
3.自适用目录分割符常量:DIRECTORY_SEPARATOR
;
4.自动加载器
<?php
//自动加载函数
spl_autoload_register(function($class){
//将类空间名称与类文件所在的路径进行映射,实现自动加载
$url=str_replace("\\",DIRECTORY_SEPARATOR,$class);
require $url.".php";
});
三、数据库操作
1.数据库
创建
create database 数据库名 collate 校验字符集;
常见校验字符集:utf8mb4_unicode_ci`选择默认数据库
use 数据库名;
查看所有数据库
show databases;
或者select
查看建库语句
show create database 数据库名;
状态(只能在命令行执行)
status
删除
drop database 数据库;
2.数据表
- 创建
create table staffs(
sid int unsigned auto_increment not null primary key,
name varchar(20) not null comment '姓名',
gender enum('male','female') not null comment '性别',
email varchar(150) not null comment '邮箱',
birthday date not null comment '生日',
create_at timestamp not null default current_timestamp comment '创建日期',
update_at timestamp not null default current_timestamp on update current_timestamp comment '更新日期'
) engine = innodb auto_increment = 1 collate = utf8mb4_unicode_ci;
结构
desc 表名;
建表语句
show create table 表名;
查看数据库中的表
show tables;
增加字段
alter table staffs add salary int unsigned not null default 2000 after gender;
更新字段
alter table staffs change salary salary float unsigned not null default 3000 after gender;
删除字段
alter table staffs drop email;
删除表
drop table staffs;
清空表数据
truncate table staffs;
更多相关文章
- 命名空间引用类的三种方式 和 命名冲突解决 与 一个自动加载类
- HashSet扩容机制在时间和空间上的浪费,远大于你的想象
- 类的重载与命名空间
- 类名的引入及自动加载类和常用mysql语句
- PHP基础: 命名空间的引入方式和自动加载类、附加:mysql的一些基
- 0223-空间引用类的三种方式,写一个自动加载类;
- PHP类的重载和命名空间(声明|调用|分解与合并|子空间和子空间的
- PHP基础知识:类的拦截器和重载以及命名空间
- 命名空间类三种引用、类导入命名冲突解决、自动加载类、mysql操