当前位置: 首页 > news >正文

沈阳网站设计制作公司网络营销专家

沈阳网站设计制作公司,网络营销专家,贵阳seo推广,机械加工网配件销售网PHP5添加了一项新的功能:Reflection。这个功能使得程序员可以reverse-engineer class, interface,function,method and extension。通过PHP代码,就可以得到某object的所有信息,并且可以和它交互。假设有一个类Person:class Person…

PHP5添加了一项新的功能:Reflection。这个功能使得程序员可以reverse-engineer class, interface,function,method and extension。通过PHP代码,就可以得到某object的所有信息,并且可以和它交互。

假设有一个类Person:

class Person {

/**

* For the sake of demonstration, we"re setting this private

*/

private $_allowDynamicAttributes = false;

/** type=primary_autoincrement */

protected $id = 0;

/** type=varchar length=255 null */

protected $name;

/** type=text null */

protected $biography;

public function getId()

{

return $this->id;

}

public function setId($v)

{

$this->id = $v;

}

public function getName()

{

return $this->name;

}

public function setName($v)

{

$this->name = $v;

}

public function getBiography()

{

return $this->biography;

}

public function setBiography($v)

{

$this->biography = $v;

}

}

通过ReflectionClass,我们可以得到Person类的以下信息:

常量 Contants

属性 Property Names

方法 Method Names

静态属性 Static Properties

命名空间 Namespace

Person类是否为final或者abstract

只要把类名"Person"传递给ReflectionClass就可以了:

$class = new ReflectionClass('Person');

获取属性(Properties):

$properties = $class->getProperties();

foreach($properties as $property) {

echo $property->getName()."n";

}

// 输出:

// _allowDynamicAttributes

// id

// name

// biography

默认情况下,ReflectionClass会获取到所有的属性,private 和 protected的也可以。如果只想获取到private属性,就要额外传个参数:

$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);

可用参数列表:

ReflectionProperty::IS_STATIC

ReflectionProperty::IS_PUBLIC

ReflectionProperty::IS_PROTECTED

ReflectionProperty::IS_PRIVATE

如果要同时获取public 和private 属性,就这样写:ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED

应该不会感觉陌生吧。

通过$property->getName()可以得到属性名,通过getDocComment可以得到写给property的注释。

foreach($properties as $property) {

if($property->isProtected()) {

$docblock = $property->getDocComment();

preg_match('/ type=([a-z_]*) /', $property->getDocComment(), $matches);

echo $matches[1]."n";

}

}

// Output:

// primary_autoincrement

// varchar

// text

有点不可思议了吧。竟然连注释都可以取到。

获取方法(methods):通过getMethods() 来获取到类的所有methods。返回的是ReflectionMethod对象的数组。不再演示。

最后通过ReflectionMethod来调用类里面的method。

$data = array("id" => 1, "name" => "Chris", "biography" => "I am am a PHP developer");

foreach($data as $key => $value) {

if(!$class->hasProperty($key)) {

throw new Exception($key." is not a valid property");

}

if(!$class->hasMethod("get".ucfirst($key))) {

throw new Exception($key." is missing a getter");

}

if(!$class->hasMethod("set".ucfirst($key))) {

throw new Exception($key." is missing a setter");

}

// Make a new object to interact with

$object = new Person();

// Get the getter method and invoke it with the value in our data array

$setter = $class->getMethod("set".ucfirst($key));

$ok = $setter->invoke($object, $value);

// Get the setter method and invoke it

$setter = $class->getMethod("get".ucfirst($key));

$objValue = $setter->invoke($object);

// Now compare

if($value == $objValue) {

echo "Getter or Setter has modified the data.n";

} else {

echo "Getter and Setter does not modify the data.n";

}

}

有点意思。

http://www.lbrq.cn/news/2666953.html

相关文章:

  • 电影网站是怎么做的上海网络推广联盟
  • 专业的网站开发公司电话优化大师下载安装免费
  • wordpress videotheme网站推广优化外包公司哪家好
  • 做外贸在什么网站最好有没有免费的seo网站
  • 酒店网站制作苏州百度推广代理商
  • 中石化第四建设有限公司网站百度网址大全 官网首页
  • 网站开发可以自学吗seo优化包括
  • 做网站包括图片设计吗深圳谷歌推广公司
  • 学做ppt的网站有哪些内容广告制作公司
  • 济南网站优化收费标准苏州网站制作开发公司
  • 公众号引流推广吸粉方案seo快速排名网站优化
  • 网站创建需要多少钱营销模式有几种
  • 湛江做网站的公司百度搜索引擎的使用方法
  • 天津百度网站排名优化网站外链代发
  • 济南免费网站建设西安seo外包公司
  • 深圳有哪些网站开发公司专业的seo搜索引擎优化培训
  • 网站建设相关行业有哪些海外seo推广公司
  • 完备的常州网站优化搜索引擎的优化方法
  • 凡科网免费建站步骤及视频如何优化seo关键词
  • 福永做网站网络营销成功的品牌
  • 在线做托福的网站怎么在百度上面打广告
  • 宁波建设协会网站首页对网站进行seo优化
  • 现在建网站做推广能赚钱吗新余seo
  • 做网站图sem竞价代运营公司
  • 建设个人网站详细点如何制作小程序
  • 网站建设业务怎么做企业培训课程有哪些
  • 虚拟主机怎么弄网站怎样在百度上发布信息
  • 什么叫域名武汉网站seo推广
  • app制作过程搜索引擎优化行业
  • 有用建站宝盒做网站的吗竞价推广课程
  • 基于python高校固定资产管理系统
  • 视图是什么?有什么用?什么时候用?MySQL中的视图
  • 【软考中级网络工程师】知识点之 RMON 技术深度剖析
  • 机器学习——模型的简单优化
  • ELK常见的问题
  • 6、图片上方添加波浪效果