座机 发表于 2017-3-30 12:35:46

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);
页: [1]
查看完整版本: php反射api基础