初学PHP反射

本文详细解析了PHP中Person类的定义与使用,包括构造函数、属性设置与获取方法及对象信息输出。通过实例展示了如何创建对象并调用其方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* person类
* 
* @since    2016-10-16
* @author    mingazi@163.com
*/
class person
{
    public $name;
    public $age;
    public function __construct($name,$age)
    {
        $this->name = $name;
        $this->age  = $age;
    }
    public function setName($name= '')
    {
        $this->name = $name;
    }
    public function setAge($age='')
    {
        $this->age = $age;
    }
    public function getUserInfo()
    {
        $info = '姓名:'.$this->name;
        $info .= ' 年龄:'.$this->age;
        return $info;
    }
}


$stu = new person('lily','22');
var_dump($stu);
$obj = new ReflectionClass('person'); // 参数是类名
Reflection::export($obj);

返回结果如下

// var_dump 返回结果
object(person)#1 (2) {
  ["name"]=>
  string(4) "lily"
  ["age"]=>
  string(2) "22"
}
// Reflection::export()返回结果
Class [ <user> class person ] {
  @@ /tmp/2c4cc08f-49d3-4584-8c82-ae4b4baf1975/code 2-25

  - Constants [0] {
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [2] {
    Property [ <default> public $name ]
    Property [ <default> public $age ]
  }

  - Methods [4] {
    Method [ <user, ctor> public method __construct ] {
      @@ /tmp/2c4cc08f-49d3-4584-8c82-ae4b4baf1975/code 6 - 10

      - Parameters [2] {
        Parameter #0 [ <required> $name ]
        Parameter #1 [ <required> $age ]
      }
    }

    Method [ <user> public method setName ] {
      @@ /tmp/2c4cc08f-49d3-4584-8c82-ae4b4baf1975/code 11 - 14

      - Parameters [1] {
        Parameter #0 [ <required> $name ]
      }
    }

    Method [ <user> public method setAge ] {
      @@ /tmp/2c4cc08f-49d3-4584-8c82-ae4b4baf1975/code 15 - 18

      - Parameters [1] {
        Parameter #0 [ <required> $age ]
      }
    }

    Method [ <user> public method getUserInfo ] {
      @@ /tmp/2c4cc08f-49d3-4584-8c82-ae4b4baf1975/code 19 - 24
    }
  }
}

转载于:https://my.oschina.net/helloworldcoding/blog/759187

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值