PHP---反射

  • 所谓反射就是定义好一个类之后,通过ReflectionClass读取类的名字从而获取类结构信息的过程
class mycoach
{
    protected $name;
    protected $age;
    public function __construct($name,$age)
    {
        $this->name = $name;
        $this->age = $age;
    }
    public function eat()
    {
        echo "嗯,吃点增加体能的东西".PHP_EOL;
    }
}
  • 拿到一个反射对象
$coach = new mycoach('陈培昌',22);
$a = new ReflectionClass('mycoach');
if($a->isInstantiable())
{
echo "科学的饮食和训练".PHP_EOL;
}

$myattributes = $a->getConstructor();
var_dump($myattributes);

输出结果:

科学的饮食和训练
object(ReflectionMethod)#3 (2) {
  ["name"]=>
  string(11) "__construct"
  ["class"]=>
  string(7) "mycoach"
}
  • 方法一  getProperties() 注意!要想实现两个属性的打印,务必在类结构体中声明两个保护属性 (例如:protected $name)
$property = $a->getProperties();
print('=================property================='.PHP_EOL);
var_dump($property);

输出结果:

=================property=================
array(2) {
  [0]=>
  object(ReflectionProperty)#4 (2) {
    ["name"]=>
    string(4) "name"
    ["class"]=>
    string(7) "mycoach"
  }
  [1]=>
  object(ReflectionProperty)#5 (2) {
    ["name"]=>
    string(3) "age"
    ["class"]=>
    string(7) "mycoach"
  }
}
  • 方法二 getMethods() getname()
print('=================methods================='.PHP_EOL);
var_dump($a->getMethods());
print('=================methods by row================='.PHP_EOL);

foreach($a->getMethods() as $method)
{
    echo $method->getName().PHP_EOL;
}

输出结果:

=================methods=================
array(2) {
  [0]=>
  object(ReflectionMethod)#6 (2) {
    ["name"]=>
    string(11) "__construct"
    ["class"]=>
    string(7) "mycoach"
  }
  [1]=>
  object(ReflectionMethod)#7 (2) {
    ["name"]=>
    string(3) "eat"
    ["class"]=>
    string(7) "mycoach"
  }
}
=================methods by row=================
__construct
eat
  • hasMethod() getMethod()
print('=================execute================='.PHP_EOL);

if($a->hasMethod('eat'))
{
    $method= $a->getMethod('eat');
    print_r($method);
}

输出结果:

=================execute=================
ReflectionMethod Object
(
    [name] => eat
    [class] => mycoach
)

Process finished with exit code 0
  • 复制粘贴---快速测试
<?php
class mycoach
{
    protected $name;
    protected $age;
    public function __construct($name,$age)
    {
        $this->name = $name;
        $this->age = $age;
    }
    public function eat()
    {
        echo "嗯,吃点增加体能的东西".PHP_EOL;
    }
}
$coach = new mycoach('陈培昌',22);
$a = new ReflectionClass('mycoach');
if($a->isInstantiable())
{
    echo "科学的饮食和训练".PHP_EOL;
}
$myattributes = $a->getConstructor();
var_dump($myattributes);
$property = $a->getProperties();
print('=================property================='.PHP_EOL);
var_dump($property);
print('=================methods================='.PHP_EOL);
var_dump($a->getMethods());
print('=================methods by row================='.PHP_EOL);

foreach($a->getMethods() as $method)
{
    echo $method->getName().PHP_EOL;
}
print('=================execute================='.PHP_EOL);

if($a->hasMethod('eat'))
{
    $method= $a->getMethod('eat');
    print_r($method);
}
原文地址:https://www.cnblogs.com/saintdingspage/p/11588609.html