php反射api基础

//反射API的demo

 

    class product{

    }

 

    class shoe extends product{

        public $a;

        private $b;

 

        function demo(){

            echo __FILE__;

        }

    }

 

    $shoeObj = new shoe();

    $refObj = new ReflectionClass('shoe'); //通过“ReflectionClass”实例化 某个类的反射类 的“对象”

    Reflection::export($refObj);                 //通过“Reflection”类的export静态方法获取某个类的具体数据

 

    $className  = $refObj->getName();        //获取被反射的类的名字

    $beObj           = $refObj->isInstantiable(); //某个类是否能被实例化

    $theObj          = $refObj->newInstance();    //实例化某个类

    $isUserDefined = $refObj->isUserDefined();  //是否用户定义的类

    $isInternal    = $refObj->isInternal();     //是否内置的类

    $fileName      = $refObj->getFileName();    //返回某个类的绝对路径,包括文件名

    $lineStart     = $refObj->getStartLine();   //获取类在文件中开始的行数

    $lineEnd       = $refObj->getEndLine();     //获取类在文件中结束的行数

 

    //获取类的源代码

    class ReflectionUtil{

        //获取某个类的源代码

        static public function getClassSource( ReflectionClass $refObj ){

            $fileName   = $refObj->getFileName();

            $lines      = @file( $fileName );  //将文件内容读取到数组中,一行一个元素,file_get_contents将内容读取到一个字符串中

            $startLine  = $refObj->getStartLine();

            $endLine    = $refObj->getEndLine();

            $len        = $endLine - $startLine +1;

            $sourceCode =  array_slice( $lines, $startLine-1, $len );

            return ($sourceCode);

        }

    }

    $sourceCode    = ReflectionUtil::getClassSource($refObj);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值