1. 程式人生 > >PHP兩個常見不常用的方法 method_exists call_user_func

PHP兩個常見不常用的方法 method_exists call_user_func

php class str log 索引 truct all array blog

method_exists判斷方法是否存在

<?php

class F{
    public function __construct(){
        if(method_exists($this, son_fun1)){
            echo son_fun1存在;
        }else{
            echo son_fun1不存在;
        }
        if(method_exists($this,son_fun2)){
            echo son_fun2存在;
        }else
{ echo son_fun2不存在; } } } class S extends F{ public function son_fun1(){ } } $a = new S();

call_user_func 動態傳入函數方法名

<?php
error_reporting(E_ALL);
function increment(&$var)
{
    $var++;
}

$a = 0;
call_user_func(‘increment‘, $a);
echo $a."\n";

// You can use this instead
call_user_func_array(‘increment‘, array(&$a));//要被傳入回調函數的數組得是索引數組。
echo $a."\n";

  

PHP兩個常見不常用的方法 method_exists call_user_func