• PHP 方法,类与对象的相关函数学习


    1、function_exists

    function_exists(string)检测函数是否存在,string表示需要检测的函数名称(注意与property_exists,method_exists,class_exists);

    <?php
    header('Content-type:text/html;charset=utf8');
    $check = 'test';
    function test($a)
    {
        echo 'this is ' . $a;
    }
    
    var_dump(function_exists('test'));
    //输出 bool(true)
    var_dump(function_exists('check'));
    //输出 bool(false)
    ?>

     2、forward_static_call 和 forward_static_call_array

    注意:这两个方法都必需在类的作用域里面调用

    forward_static_call(callable,string...)callable表示需要调用的静态方法,一般的写法为array(className,staticName),string表示需要传入的参数,可以为多个参数

    forward_static_call_array(callable,array)用法和上面一样,但是参数是一个数组

    <?php
    header('Content-type:text/html;charset=utf8');
    
    Class A
    {
        public static function test()
        {
            var_dump(func_get_args());
        }
    }
    
    Class B
    {
        public static function check()
        {
            forward_static_call(['A', 'test'], 'first', 'second');
    //        forward_static_call([new A, 'test'], 'first', 'second');   这个方法也可以执行,但是推荐上面那种写法
        }
    
        public static function take()
        {
            forward_static_call_array(['A', 'test'], ['a', 'b', 'c']);
        }
    }
    
    B::check();
    //输出 array(2) { [0]=> string(5) "first" [1]=> string(6) "second" }
    B::take();
    //输出 array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" }
    ?>

     3、call_user_func 和 call_user_func_array

    这两个方法表示调用方法

    call_user_func(callable,string...) 表示调用已定义的方法,string表示需要传入的参数,如果调用的是类里面的方法,那么就要用call_user_func([className,funcName],string...)

    call_user_func_array(callable,array) 用法同上面的一样,但是该方法传数的参数是一个数组

    注意如果在类里面调用非静态方法传处call_user_func_array([$this,$method_name],$params),如果在类里面调用静态方法传入call_user_func_array(['self','method_name'],$params);

    <?php
    header('Content-type:text/html;charset=utf8');
    
    Class A
    {
        public function test()
        {
            var_dump(func_get_args());
        }
    }
    
    Class B
    {
        public function check()
        {
            var_dump(func_get_args());
        }
    }
    
    function method($v = null)
    {
        echo 'this is method ' . $v;
    }
    
    call_user_func('method', 'haha');
    //输出 this is method haha
    call_user_func(['A', 'test'], 'a', 'b', 'c', 'd');
    //输出 array(4) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" }
    call_user_func_array(['B', 'check'], ['first', 'second', 'third']);
    //输出 array(3) { [0]=> string(5) "first" [1]=> string(6) "second" [2]=> string(5) "third" }
    ?>

     4、func_get_arg,func_get_args 和 func_num_args

    func_get_arg(num)表示返回参数列表中的指定参数,num表示参数的偏移量

    func_get_args()表示返回所有的参数

    func_num_args()表示返回所有参数的个数

    <?php
    header('Content-type:text/html;charset=utf8');
    
    Class A
    {
        public function test()
        {
            var_dump(func_get_args());
            //输出 array(3) { [0]=> string(5) "first" [1]=> string(6) "second" [2]=> string(5) "third" }
            var_dump(func_get_arg(0));
            //输出 string(5) "first"
            var_dump(func_num_args());
            //输出 int(3)
        }
    }
    
    call_user_func([new A, 'test'], 'first', 'second', 'third');
    ?>

     5、get_defined_functions

    get_defined_functions()表示返回所有的函数,包括系统内部的函数,用户自定义的函数,系统内部的函数用$arr['internal']来访问,用户的函数用$arr['user']来访问

    <?php
    header('Content-type:text/html;charset=utf8');
    
    function test()
    {
        echo 'this is test';
    }
    
    print_r(get_defined_functions());
    //输出如下数组
    //Array
    //(
    //    [internal] => Array
    //    (
    //        [0] => zend_version
    //        [1] => func_num_args
    //        [2] => func_get_arg
    //        [3] => func_get_args
    //        [4] => strlen
    //        ...
    //        [750] => bcscale
    //        [751] => bccomp
    //    )
    //
    //    [user] => Array
    //(
    //    [0] => test
    //)
    //
    //)
    ?>

    5、register_shutdown_function

    register_shutdown_function(callback,params)表示定义一个在页面加载完之后触发(即也是在exit前触发的函数),callback表示回调函数,params表示需要传入的函数,也可以调用类里面的方法,方法同call_user_func()一样

    <?php
    header('Content-type:text/html;charset=utf8');
    register_shutdown_function(function () {
        var_dump(func_get_args());
    }, 'y', 'f');
    
    function test()
    {
        echo 'this is first'."<br>";
        exit;
        echo 'this is second';
    }
    
    test();
    //输出如下内容
    //this is first
    //array(2) { [0]=> string(1) "y" [1]=> string(1) "f" }
    ?>
    
    
    <?php
    header('Content-type:text/html;charset=utf8');
    register_shutdown_function([new A, 'test'], 'AA', 'BB', 'CC');
    
    Class A
    {
        public function test()
        {
            var_dump(func_get_args());
        }
    }
    
    echo 'ok <br>';
    //输出如下内容
    //ok
    //array(3) { [0]=> string(2) "AA" [1]=> string(2) "BB" [2]=> string(2) "CC" }
    ?>
  • 相关阅读:
    Dynamics CRM for Outlook问题集
    Dynamics CRM
    VMWare安装CentOS-6.3-x86_64-minimal和LAMP
    Microsoft Dynamics CRM Update Rollup
    Reporting Service 迁移: 从2005到2008
    [转]非常好的vsftpd安装于配置
    关闭水滴直播平台 周鸿祎曾态度强硬
    贾跃亭此前曾公开表示,“法拉第未来计划于2
    爆出的法拉第未来(Faraday Future,以下简称“FF”)
    YII2笔记之三
  • 原文地址:https://www.cnblogs.com/rickyctbu/p/9860456.html
Copyright © 2020-2023  润新知