方法一:
- $r = new ReflectionClass($this);
- Zend_Debug::dump($r->getConstants(), "Constants");
- Zend_Debug::dump($r->getProperties(), "Properties");
- Zend_Debug::dump($r->getMethods(), "Methods");
方法二:
- <?php
- class myclass {
- public $val1 = 1;
- protected $val2 = 2;
- private $val3 = 3;
- }
- $foo = new myclass();
- $reflect = new ReflectionClass($foo);
- $pros = $reflect->getDefaultProperties();
- var_dump($pros);
- ?>
方法三:
外部只能拿到public的变量
补充一个全部获取的方法
- class test {
- public $val1 = 1;
- protected $val2 = 2;
- private $val3 = 3;
- public function getVar()
- {
- return get_class_vars(get_class($this));
- }
- }
- $my_class = new test();
- var_dump($my_class->getVar());
方法四:
- class test {
- public $val1 = 1;
- protected $val2 = 2;
- private $val3 = 3;
- }
- class testChild extends test
- {
- public function getFeilds()
- {
- $tt = new test();
- $class_vars = get_class_vars(get_class($tt));
- var_dump($class_vars);
- }
- }
- testChild::getFeilds();
我们这边之前使用的一个框架就采用了类似的方法,结果产生了基类的private方法暴露的问题
这个不知道算不算是php的一个bug,反正在类似使用时要特别小心吧
没有评论:
发表评论