五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > PHP函數(shù)method_exists檢查類是否存在和與is_callable()的區(qū)別

PHP函數(shù)method_exists檢查類是否存在和與is_callable()的區(qū)別

文章來(lái)源:365jz.com     點(diǎn)擊數(shù):1458    更新時(shí)間:2018-08-26 08:45   參與評(píng)論

PHP method_exists 檢查類的方法是否存在

method_exists

method_exists — 檢查類的方法是否存在

說(shuō)明

bool method_exists ( object $object , string $method_name )

如果 method_name 所指的方法在 object 所指的對(duì)象類中已定義,則返回 TRUE,否則返回 FALSE。

Example #1 method_exists() 例子

<?php
$directory 
= new Directory('.');
var_dump(method_exists($directory,'read'));
?>

以上例程會(huì)輸出:

bool(true)


PHP method_exists note #1

Just to mention it: both method_exists() and is_callable() return true for inherited methods:

<?php
class ParentClass {

   function 
doParent() { }
}

class 
ChildClass extends ParentClass { }

$p = new ParentClass();
$c = new ChildClass();

// all return true
var_dump(method_exists($p'doParent'));
var_dump(method_exists($c'doParent'));

var_dump(is_callable(array($p'doParent')));
var_dump(is_callable(array($c'doParent')));
?>

PHP method_exists note #2

Using method_exists inside an object's __call() method can be very usefull if you want to avoid to get a fatal error because of a limit in function nesting or if you are calling methods that dont exist but need to continue in your application: 

<?php 
class Something 


    
/** 
     * Call a method dynamically 
     * 
     * @param string $method 
     * @param array $args 
     * @return mixed 
     */ 
    
public function __call($method$args
    { 
        if(
method_exists($this$method)) { 
          return 
call_user_func_array(array($this$method), $args); 
        }else{ 
          throw new 
Exception(sprintf('The required method "%s" does not exist for %s'$methodget_class($this))); 
        } 
    } 


?>

PHP method_exists note #3

As noted [elsewhere] method_exists() does not care about the existence of __call(), whereas is_callable() does: 

<?php 
class Test 
  public function 
explicit(  ) { 
      
// ... 
  

   
  public function 
__call$meth$args ) { 
      
// ... 
  



$Tester = new Test(); 

var_export(method_exists($Tester'anything')); // false 
var_export(is_callable(array($Tester'anything'))); // true 
?>

PHP method_exists note #4

Be warned that the class must exist before calling method exists from an eval statement otherwise you will get a Signal Bus error.

PHP method_exists note #5

It wasn't spelled out but could be inferred: method_exists() also works on interfaces.

<?php

var_dump
(method_exists("Iterator""current"));
// bool(true)

?>

PHP method_exists note #6

If you want to check in a class itself if a method is known you may do so with magic variable __CLASS__

<?php

class A{
  
__construct($method){
      return 
method_exists(__CLASS__,$method);
  }

  private function 
foo(){
  
  }
}

$test = new A('foo');
//should return true

?>

You might also use the method describe below with <?php in_array() ?>trick but I consider this one here easier and more readable and well, the way it is intended toi be done ;)

PHP method_exists note #7

Hi, 

Here is a useful function that  you can use to check classes methods access e.g whether it is public, private or static or both.. 

here it goes: 

<?php 
// Example class 
class myClass 

    private 
$private1
    
    static 
$static1
    
    public 
$public1
        
    
    public function 
publ() { 
    
    } 
    
    private function 
priv() { 
    
    } 
    
    private static function 
privstatic() { 

    } 
    
    public static function 
publstatic() { 
    
    } 
    
    static function 
mytest() { 
    
    } 


// The function uses the reflection class that is built into PHP!!! 
// The purpose is to determine the type of a certain method that exi 
function is_class_method($type="public"$method$class) { 
   
// $type = mb_strtolower($type); 
    
$refl = new ReflectionMethod($class$method); 
    switch(
$type) { 
        case 
"static"
        return 
$refl->isStatic(); 
        break; 
        case 
"public"
        return 
$refl->isPublic(); 
        break; 
        case 
"private"
        return 
$refl->isPrivate(); 
        break; 
    } 

var_dump(is_class_method("static""privstatic""myClass")); // true - the method is  private and also static.. 
var_dump(is_class_method("private""privstatic""myClass")); // true - the method is  private and also static.. 
var_dump(is_class_method("private""publstatic""myClass")); // False the methos is public and also static not private 
 // you get the idea.. I hope this helps someone.. 
?>

PHP method_exists note #8

This function is case-insensitive (as is PHP) and here is the proof:
<?php
class {
    public function 
FUNC() { echo '*****'; }
}

$a = new A();
$a->func(); // *****
var_dump(method_exists($a'func')); // bool(true)
?>

PHP method_exists note #9

As mentioned before, is_callable and method_exists report all methods callable even if they are private/protected and thus actually not callable. So instead of those functions you may use following work-around which reports methods as supposed to.

<?php
class Foo1 {
  public function 
bar() {
    echo 
"I'm private Foo1::bar()";
  }
}

class 
Foo2 {
  private function 
bar() {
    echo 
"I'm public Foo2::bar()";
  }
}

$f1=new Foo1;
$f2=new Foo2;

if(
is_callable(array($f1,"bar"))) {
    echo 
"Foo1::bar() is callable";
} else {
    echo 
"Foo1::bar() isn't callable";
}
if(
is_callable(array($f2,"bar"))) {
    echo 
"Foo2::bar() is callable";
} else {
    echo 
"Foo2::bar() isn't callable";
}
if(
in_array("bar",get_class_methods($f1))) {
    echo 
"Foo1::bar() is callable";
} else {
    echo 
"Foo1::bar() isn't callable";
}
if(
in_array("bar",get_class_methods($f2))) {
    echo 
"Foo2::bar() is callable";
} else {
    echo 
"Foo2::bar() isn't callable";
}

?>

output
Foo1::bar() is callable (correct)
Foo2::bar() is callable (incorrect)
Foo1::bar() is callable (correct)
Foo2::bar() isn't callable (correct)

?>


在編程中,我們有的時(shí)候需要判斷某個(gè)類中是否包含某個(gè)方法,除了使用反射機(jī)制,PHP還提供了method_exists()和is_callable()方法進(jìn)行判斷。那么兩則區(qū)別是什么呢?

已知類文件如下:

class Student{
    private $alias=null;    private $name='';    public function __construct($name){
        $this->name=$name;
    }    private function setAlias($alias){
        $this->alias=$alias;
    }    public function getName(){
        return $this->name;
    }
}12345678910111213

當(dāng)方法是private,protected類型的,method_exists會(huì)報(bào)錯(cuò),is_callable會(huì)返回false。

實(shí)例

下面是判斷某一對(duì)象中是否存在方法getName

通過(guò)method_exists實(shí)現(xiàn)

$xiaoming=new Student('xiaoming');if (method_exists($xiaoming, 'getName')) {   
    echo 'exist';
}else{    echo 'not exist';
}exit();1234567

輸出exist

通過(guò)is_callable實(shí)現(xiàn)

$xiaoming=new Student('xiaoming');if (is_callable(array($xiaoming, 'getName'))) {   
    echo 'exist';
}else{    echo 'not exist';
}exit();1234567

輸出exist

下面是判斷某一對(duì)象中是否存在方法setAlias 
當(dāng)使用method_exists的時(shí)候報(bào)錯(cuò)如下 
 20160810172253227.jpg

當(dāng)使用is_callable的時(shí)候,輸出not exist


如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答!! 點(diǎn)擊進(jìn)入論壇

發(fā)表評(píng)論 (1458人查看0條評(píng)論)
請(qǐng)自覺(jué)遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
昵稱:
最新評(píng)論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)