php类知识----特别用法

  • spl_autoload_register注册
<?php

#spl_autoload_register-----这个例子是用来打印实例化类的类名
function thereisagameoflove($classname)  #自定义函数 thereisagameoflove参数$classname是类的名字
{
    echo $classname;  #打印类的名字
    //找到类文件并导入
    include "wenwa.php";   #下面use后面的类 定义在文件wenwa.php中
}
spl_autoload_register('thereisagameoflove');
use \trainningplan2\cj\mycoach; 
$cpc = new mycoach();
?>

输出结果:

trainningplan2\cj\mycoach

  •  call_user_func  通过在参数中输入实例对象名,对象方法名,参数达到执行函数的目的 call_user_func_array([对象名,方法名],对象方法参数)

 

<?php
class mycoach
{
    public function __construct($name,$age)
    {
        $this->name = $name;
        $this->age = $age;
    }
    public function introduce($name,$age)
    {
        echo "我是 ".$name." 今年 ".$age."\n";
    }
    public function saymorning($name)
    {
        echo "good morning~ i'm ".$name;
    }
}
$cj = new mycoach('程劲',20);
call_user_func([$cj,'saymorning'],'劲儿弟弟');
?>

 

输出结果:

good morning~ i'm 劲儿弟弟

 

  •  call_user_func_array 通过在参数中输入实例对象名,对象方法名,参数达到执行函数的目的 call_user_func_array([对象名,方法名],[对象方法参数1,对象方法参数2,......])
<?php
class mycoach
{
public function __construct($name,$age)
{
$this->name = $name;
$this->age = $age;
}
public function introduce($name,$age)
{
echo "我是 ".$name." 今年 ".$age."\n";
}
}
$cj = new mycoach('程劲',20);
call_user_func_array([$cj,'introduce'],['程劲',20])
?>

 输出结果:

我是 程劲 今年 20

  • 对于普通函数,call_user_func_array, call_user_func可以这么用
<?php
function introduce($name)
{
    echo "我是 ".$name."\n";
}
function expertin($expert1,$expert2)
{
    echo "擅长格斗技:".$expert1.",".$expert2;
}
call_user_func('introduce','劲儿弟弟');
call_user_func_array('expertin',['泰拳','巴西柔术']);
?>

输出结果:

我是 劲儿弟弟
擅长格斗技:泰拳,巴西柔术

  • 其他用法

 

posted @ 2019-06-02 22:38  saintdingtheGreat  阅读(173)  评论(0编辑  收藏  举报