• php反射函数


    最常用的几个个php反射函数 get_class get_class_methods

    1. get_class — 返回对象的类名

    string get_class ([ object $obj ] )

    返回对象实例 obj 所属类的名字。如果 obj 不是一个对象则返回 FALSE

    Note: 在 PHP 扩展库中定义的类返回其原始定义的名字。在 PHP 4 中 get_class() 返回用户定义的类名的小写形式,但是在 PHP 5 中将返回类名定义时的名字,如同扩展库中的类名一样。

    Note: 自 PHP 5 起,如果在对象的方法中调用则 obj 为可选项。

    Example#1 使用 get_class()

    <?php

    class foo {
        function 
    foo()
        {
        
    // implements some logic
        
    }

        function 
    name()
        {
            echo 
    "My name is " get_class($this) , "/n";
        }
    }

    // create an object
    $bar = new foo();

    // external call
    echo "Its name is " get_class($bar) , "/n";

    // internal call
    $bar->name();

    ?>

    上例将输出:

    Its name is foo My name is foo
     
    2.get_class_methods — 返回由类的方法名组成的数组

    说明

    array get_class_methods ( mixed $class_name )

    返回由 class_name 指定的类中定义的方法名所组成的数组。如果出错,则返回 NULL

     

    Example#1 get_class_methods() 示例

    <?php

    class myclass {
        
    // constructor
        
    function myclass()
        {
            return(
    true);
        }
        
        
    // method 1
        
    function myfunc1()
        {
            return(
    true);
        }

        
    // method 2
        
    function myfunc2()
        {
            return(
    true);
        }
    }

    $class_methods get_class_methods('myclass');
    // or
    $class_methods get_class_methods(new myclass());

    foreach (
    $class_methods as $method_name) {
        echo 
    "$method_name/n";
    }

    ?>

    上例将输出:

    myclass myfunc1 myfunc2 
     
     
     
    3.

    get_class_vars — 返回由类的默认属性组成的数组

    说明

    array get_class_vars ( string $class_name )

    返回由类的默认公有属性组成的关联数组,此数组的元素以 varname => value 的形式存在。

    Note: 在 PHP 4.2.0 之前,get_class_vars() 不会包含未初始化的类变量。

    Example#1 get_class_vars() 示例

    <?php

    class myclass {

        var 
    $var1// 此变量没有默认值……
        
    var $var2 "xyz";
        var 
    $var3 100;
        private 
    $var4// PHP 5

        // constructor
        
    function myclass() {
            
    // change some properties
            
    $this->var1 "foo";
            
    $this->var2 "bar";
            return 
    true;
        }

    }

    $my_class = new myclass();

    $class_vars get_class_vars(get_class($my_class));

    foreach (
    $class_vars as $name => $value) {
        echo 
    "$name : $value/n";
    }

    ?>

    上例将输出:

    // 在 PHP 4.2.0 之前 var2 : xyz var3 : 100 // 从 PHP 4.2.0 开始 var1 : var2 : xyz var3 : 100 
     
  • 相关阅读:
    网站前台性能优化教程
    解决Jboss打开run.bat时闪退不能启动的方法
    如何讲解自己开发的程序
    数据库调优教程汇总
    数据库调优教程(十三) MySQL数据库其他优化方法
    数据库调优教程(十二) 优化sql语句
    数据库调优教程(十一) 设计一张漂亮的表
    数据库调优教程(十) 【精华章节】解决like ’%str’ 时索引不被使用的4种方法
    数据库调优教程(九) 添加了索引但不被使用的几种常见可能
    Redis Cluster 实践
  • 原文地址:https://www.cnblogs.com/MichaelZhangX/p/2415679.html
Copyright © 2020-2023  润新知