• 对象和工具


    • 类函数和对象函数

    查找类
    1. bool class_exists ( string $class_name [, bool $autoload = true ] );
      它是有两个参数的,我们平时用这个方法的时候大都只给了第一个参数,第二个参数的默认值是默认为true,而关于第二个参数的解释是:
      当我们不设置第二个参数时,会去调用__autoload方法去加载类
    2. array get_declared_classes()
      列出用户定义的类和php内置的类
    了解对象或类
      1. get_class(obj)
        检测对象的类,返回字符串类型的类名

         instanceof

         检测对象是否属于某个家族
    <?php
    abstract class base{
        abstract function init();
    }
    
    class A extends base{
        function init(){
            echo 1111;
        }
    }
    
    $a = new A();
    //echo get_class($a);//A
    var_dump($a instanceof base);//true
    ?>
    了解对象或类
    1. array get_class_methods(string $class/obj $obj)
    得到类中的所有方法列表
    1. is_callble()
    检测参数是否为合法的可调用结构
    <?php
    class test {
        static function a() {
            return "test";
        }
    }
    class abc {
        public function a($Object,$funcName){    
            if (!is_callable(array($Object,$funcName))){//此处array($object,$funcname)的用法,相当于$object::$funcname调用$object的$funcname方法
                echo("Error");
            }else {
                echo("OK");
            }
        }
    }
     
    $abc=new abc();
    $abc->a("test","aa");//test类下没有aa方法,会输出error
    $abc->a("test","a");//test类下有a方法,会输出ok
    ?>
    1. method_exists
    了解类中的方法
    1. get_class_vars
    了解类中的集成
    1. get_parent_class(class/obj)

    is_subclass_of

    方法调用
    1. call_user_func()
    2. call_user_func_array()
    • 反射API

    1. 检查类    reflectionclass()
    <?php
    class product{
    
    }
    
    class cdProduct extends product{
        public static function dumpDeatil(ReflectionClass $ReflectionClass){
            $detail ='';
            $name = $ReflectionClass->getname();
            $path = $ReflectionClass->getFileName();
            $detail .="$name FileName is $path<br>";
            $from = $ReflectionClass->getStartLine();
            $detail .="$name StartLine is $from<br>";
            $to = $ReflectionClass->getEndLine();
            $detail .="$name EndLine is $to<br>";
            if($ReflectionClass->isUserDefined()){//判断是否为用户自定义的类
                $detail .="$name is user defined<br>";
            }
            if($ReflectionClass->isInternal()){//判断是否为内置类
                $detail .="$name is built-in<br>";
            }
            if($ReflectionClass->isInterface()){//判断类是否为借口
                $detail .="$name is Interface<br>";
            }if($ReflectionClass->isAbstract()){//判断是否为抽象的
                $detail .="$name is Abstract<br>";
            }
            if($ReflectionClass->isFinal()){//判断能否被继承
                $detail .="$name is Final<br>";
            }
            if($ReflectionClass->isInstantiable()){//能否得到类的实例
                $detail .="$name can be Instantiable<br>";
            }else{
                $detail .="$name can not be Instantiable<br>";
            }
            return $detail;
        }
    
    
    }
    
    echo cdProduct::dumpDeatil(new ReflectionClass('cdProduct'));
    /*
    cdProduct FileName is D:\test\reflectuonclass.php
    cdProduct StartLine is 6
    cdProduct EndLine is 39
    cdProduct is user defined
    cdProduct can be Instantiable
    */
    ?>
    1. 检查方法  reflectionmethod()
    2. 检查参数  getparameters()
    综合应用
    <?php
    
    
    
    interface Module{
        function execute();
    }
    
    
    
    class FtpModule implements Module{
        function setHost($host){
            print("FtpModule::setHost() :$host\n");
        }
    
        function setUser($user){
            print("FtpModule::setUser() :$user\n");
        }
    
        function execute(){
    
        }
    }
    
    
    class Person{
        public $name;
        function __construct($name)
        {
            $this->name = $name;
        }
    }
    
    class PersonModule implements Module{
    
        function setPerson(Person $person){
            print("PersonModule::setPerson():{$person->name}\n");
        }
    
        function execute(){
    
        }
    
    }
    
    
    class ModuleRunner{
        private $configData = array(
            'PersonModule'=>array('person'=>'bob'),
            'FtpModule'=>array('host'=>'example.com','user'=>'anon')
        );
    
        private $Modules = array();
    
    
        function init(){
            $interface = new ReflectionClass('Module');
            foreach ($this->configData as $k => $v) {
                $module_class = new ReflectionClass($k);
                if(!$module_class->isSubclassof($interface)){//确保模块属于Modules类型
                    throw new Exception("unknown nodule type:$k");
                }
                $model = $module_class->newInstance();//创建Module对象的实例
                foreach ($module_class->getMethods() as $method) {
                    $this->handleMethod($model,$method,$v);
                }
                array_push($this->Modules,$model);
            }
        }
    
    
        function handleMethod(Module $moudel,ReflectionMethod $method,$params){
            
            $name = $method->getName();//获取函数名
            $args = $method->getParameters();//获取参数
            if(count($args)!=1 || substr($name,0,3)!='set'){//判断参数个数并且是以set命名开始的方法
                return false;
            }
            $property = strtolower(substr($name,3));//获取属性名
    
            if(!isset($params[$property])){//判断参数是否存在
                return false;
            }
    
            $arg_class = $args[0]->getClass();//检查参数数据类型
            if(empty($arg_class)){//为空则为基本数据类型
                $method->invoke($moudel,$params[$property]);
            }else{//否则为一个对象
                $method->invoke($moudel,$arg_class->newInstance($params[$property]));
            }
    
        }
    
    }
    
    $test = new ModuleRunner();
    $test->init();
    
    ?>
  • 相关阅读:
    [LeetCode] 1160. Find Words That Can Be Formed by Characters
    [LeetCode] 561. Array Partition I
    [LeetCode] 942. DI String Match
    [LeetCode] 852. Peak Index in a Mountain Array
    [LeetCode] 461. Hamming Distance
    [LeetCode] 617. Merge Two Binary Trees
    SSM项目实现连接两个mysql数据库
    springboot导入excel到mysql
    Mysql修改表备注, 列信息
    sql.xml where ids in的写法
  • 原文地址:https://www.cnblogs.com/rcjtom/p/6050889.html
Copyright © 2020-2023  润新知