几个常用的检测类和对象的方法

<?php
    interface Person{
        
    }
    class Computer{
        public $name;
        public function _run(){
            
        }
    }
    class NoteComputer extends Computer{
        
    }
    $com1 = new Computer();
    $note1 = new NoteComputer();
    //1 检查类是否存在,存在返回true
    echo class_exists(Computer); 
    //2 获取对象的类名,不是对象返回false
    echo get_class($com1);
    //3 获取类中的公共方法,返回数组
    print_r(get_class_methods(Computer));
    //4 获取类中的公共字段,返回数组
    print_r(get_class_vars(Computer));
    //5 获取子类的父类,没有返回flase
    echo get_parent_class(NoteComputer); //可以放类名或对象
    echo get_parent_class($note1);
    //6 判断接口是否存在
    echo interface_exists(Person);
    //7 判断对象是否属于这个类,或者这个类的父类
    echo is_a($note1, NoteComputer);
    echo is_a($note1, Computer);
    //8 确定对象是否是类的子类
    echo is_subclass_of($note1, Computer);
    //9 确定类里面有没有这个方法
    echo method_exists(Computer, _run);
?>

 

posted @ 2016-12-26 11:36  党兴明  阅读(342)  评论(0编辑  收藏  举报