zoukankan      html  css  js  c++  java
  • 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 	rainningplan2cjmycoach; 
    $cpc = new mycoach();
    ?>

    输出结果:

    trainningplan2cjmycoach

    •  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."
    ";
        }
        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." ";
    }
    }
    $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."
    ";
    }
    function expertin($expert1,$expert2)
    {
        echo "擅长格斗技:".$expert1.",".$expert2;
    }
    call_user_func('introduce','劲儿弟弟');
    call_user_func_array('expertin',['泰拳','巴西柔术']);
    ?>

    输出结果:

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

    • 其他用法

  • 相关阅读:
    JavaScript对象的几种创建方式?
    TCP 三次握手,四次挥手
    常用的状态码
    前后端分离的接口规范
    京东架构师:日均 5 亿查询量的ElasticSearch架构如何设计?
    [转] 谈谈Spring中都用到了那些设计模式?
    [转]Post和Get的区别
    [转]17个常用的JVM参数
    从入门到熟悉HTTPS的9个问题
    布式事务和解决方案理论
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/10964851.html
Copyright © 2011-2022 走看看