1.trait可以实现类的多继承,PHP本身不支持类的多继承(单继承)。
2.final关键字:类不希望被子类继承或者方法重写
3. 命名空间
define:常量的定义
const :类常量的定义
namespace use
use function verdor/OwnFun;//使用命名空间引入函数
4.类的自动加载
(1).function __autoload($className){
require $className.".php";
}
(2).第一种:直接加载
spl_autoload_register(function autoload($className){
require $className.".php";
});
第二种:函数加载
function autoload($className){
require $className.".php";
}
spl_autoload_register('autoload');//直接传函数名
第三种:类加载
class ClassName{
function autoload($className){
require $className.".php";
}
}
spl_autoload_register([new ClassName,'autoload']);
5.static关键字
class Person{
public static $test = 'yang';
pubic static function test(){
echo (self::$test);//类中静态变量的调用
return "测试案例";
}
}
echo Person::$test;//类中静态变量的调用
echo Person::test();//类中静态函数的调用
class Man extends Person{
public function man(){
parent::test();//调用父类中的静态函数
}
}
static::fun();//后期静态类的绑定
self::fun(); //调用父类中的静态方法
6. 魔术方法
__set() __get()
__isset() __unset()
__call():类中函数不存在时调用
public function __call($fun,$arg){
echo ($fun);
print_r($arg);
}
__callStatic():类中静态函数不存在时调用
__invoke():对象按函数使用
__toString():类的对象转换成字符串
7. 对象复制
类对象复制是拷贝的关系
clone禁止了对象之间的拷贝
8.类的约束:
class A{
pubic function test(){
return "test";
}
}
function test(A $a){
echo $a->test();
}
test(new A());
9.接口
interface Person{//定义接口
public function test();
}
class Man extends Person{//接口实现
public function test(){
echo "test";
}
}
class Yang{//工厂模式
public static function factory(Person $user){
return $user;
}
}
$user = Yang::factory(new Man());
$user->test();
10.抽象类:abstract类中可存在自己的方法
11.单例模式
class Test{
private static $_instance = null;
private function __construct(){
}
private function __clone(){
}
public static function getInstance(){
if(!self::$_instance instanceof self){
self::$_instance = new self();
}
}
}
12.工厂模式
class Factory{
public static function factory($className,$object){
return new $className;
}
}
$factory = Factory::factory();
13.include和require的区别?
(1).include失败时会产生几个警告(warning),require直接中断程序,并且是在程序运行时载入
(2).require在运行前进行载入 inlcude_once():单次引入 reqiure_once:单次引入
14.htmlspecialchars():预定义的字符转换为html实体
15.常用的魔术方法:
(1)__construct():构造方法 实例化类时自动调用
(2)__set():为定义属性赋值
(3)__get():属性调用
(4)__isset() __unset() __sleep() __call() __callStatic()
__toString():对象转 __clone():复制一个对象
16.PHP遍历文件夹下的所有文件
17.PHP中使用到的数组:
array_values():返回数组中所有的值-建立数组索引
array_keys():返回数组中的所有键值
array_chunk():将一个数组分割成多个---最后一个数组可能会少于这个数组个数
18.final:父类中的方法被声明final之后,子类无法覆盖该方法(不能被继承)---只有类和方法才能被定义为 final。
19.声明类属性或方法为静态,就可以不实例化类而直接访问。静态属性不能通过一个已经实例化的对象来访问(静态方法可以)
$this在静态方法中不可用!
20.redis涉及到五种数据类型:
(1).字符串 string
(2).哈希 hash
(3).列表 list
(4).集合 set
(5).有序集合 Zset
字符串:
设置一个值:set key value
获取一个值:get key
自增1 :incr key
自减1 :decr key
获取所有的键:keys *
删除键 :del key
固定值自增:incrby key value
固定值自减:decrby key value
设置多个键值对:mset key value key1 value1 key2 value2
hash:
添加fields-value值:hset hash field 11
获取fields值: hget hash field
获取key值下的所有的field:hkeys hash
获取key值下的值:hvals hash
获取key至下面的所有field-value键值对:hgetall hash
删除指定key值下的field值:hdel hash field
列表: 有序可重复
左追加:lpush list value value1
又追加:rpush list value value1
获取列表中所有的值:lrange list 0 -1
左移除:lpop list
右移除:rpop list
集合:无序不重复
添加key:sadd set a b c
获取key:smembers set
删除key:srem set a/不写代表全部
集合运算:
sadd seta a b c d
sadd setb b d e f
差集 sdiff seta setb 结果:a c
交集 sinter seta setb 结果:b d
并集 sunion seta setb 结果:a b c d e f
有序集合:不重复(排序去重资源消耗大)
添加值:zadd zset 分数1 value1 分数2 value2
过期时间设置:
设置过期时间:expire key second(秒)
查看过期剩余时间:ttl key
清除过期时间:persist key
21.PHP数组:
array_change_key_case():所有键名全改为大写或小写
array_chunk():分隔多个数组
array_column():返回数组指定列
array_combine():创建一个新的数组,一个数组的值作为键一个作为值
array_count_values():统计数组中的所有值
array_diff_assoc():键值对数组形式的差集
array_diff_key():键名比较计算数组的差集
array_diff($arr,$brr):计算数组中的差集==在arr不在brr
array_fill_keys():使用指定的键和值填充数组
array_fill(start_index,num,value):用给定的值填充数组
array_filter($arr,'回调函数'):回调函数过滤数组中的单元
array_flip():交换数组中的键和值
end():获取数组最后一个元素
current():获取第一个元素
prev():获取上一个元素
array_push():添加元素
array_pop():弹出元素
array_key_exists():检查数组离是否存在指定的键名
array_key_first():获取第一个元素的键名
array_key_last():获取最后一个元素的键名
array_keys():获取数组中的键值
array_map():为每个元素回调函数
array_merge():合并数组
arrar_multisort():对多个数组或多位数组进行排序
array_pad():插入元素
array_product():计算数组中所有值得乘机
array_rand():从数组中随机去除一个或多个单元
count():获取数组的长度
array_replace():后面数组的值替换第一个数组的值
array_reverse():以相反的顺序返回数组
array_search():搜索数组中给定的值并返回键名
array_shift():删除数组中首个元素并返回被删除元素的值
array_slice():删除并替换数组中指定的元素
array_sum():返回数组值的和
array_unique():去重
array_unshift():在数组头插入一个或多个元素
array_values():返回数组中所有的值
array_walk():对数组中每个成员应用函数
array():数组
in_array():是否在数组中
key():关联数组中取得键名
list():把数组中的值赋给变量
22.字符串:
uwords():将字符串的每个单词的首字母转换为大写
addslashes():返回转以后的字符
chop():删除字符串右侧的空白或其他字符串
chr():返回指定的字符
chunk_split():将字符串分割成小块
explode():字符串转换为数组
implode():数组转字符串
lcfirst():字符串的首字母大写
ltrim():移出左侧空白或者其他字符
md5():加密
str_replace():替换字符串
str_repeat():重复字符串次数
str_split():字符分割到数组
stripos():返回字符串在另一个字符串中出现的位置
stristr():查找字符串在另一个字符串首次出现的位置
strlen():返回字符串的长度
strpos():查找字符串在另个字符串中出现的位置
strrchr():查找字符串在另一个字符串最后一次出现的位置
strrev():字符串反转
strtolower():字符串转换为小写
strtoupper():字符串转换为大写
substr():返回字符串的一部分
substr_count():计算淄川在字符串中出现的次数
substr_replace():字符串替换
ucfirst():字符串首字母大写