- get_object_vars函数,将对象中有访问权限的的字段转为数组
<?php class foo { private $a; public $b = 1; public $c; private $d; static $e; public function test() { var_dump(get_object_vars($this)); } } $test = new foo; var_dump(get_object_vars($test)); $test->test(); ?>
结果
array(2) { ["b"]=> int(1) ["c"]=> NULL } array(4) { ["a"]=> NULL ["b"]=> int(1) ["c"]=> NULL ["d"]=> NULL }
2.array get_class_methods ( mixed $class_name )与array get_class_vars ( string $class_name )
得到类中的所有可访问的方法
<?php class myclass { // constructor public function myclass() { return(true); } // method 1 protected function myfunc1() { return(true); } // method 2 private function myfunc2() { return(true); } public function description() { $class_methods = get_class_methods($this); //or $class_methods = get_class_methods(__CLASS__); foreach ($class_methods as $method_name) { echo "$method_name\n"; } } } $class_methods = get_class_methods(‘myclass‘); //得到所有公开的方法名称 foreach ($class_methods as $method_name) { echo "$method_name\n"; } //结果是==》myclass description $class_methods = get_class_methods(new myclass()); //得到所有公开的方法名称,注意,构造方法为private或者protected时报错 foreach ($class_methods as $method_name) { echo "$method_name\n"; } //结果是==》myclass description $t = new myclass(); $t->description(); //结果是==》myclass myfunc1 myfunc2 description ?>
2.__call魔术方法
在php官方描述中是调用不存在的方法时调用,这个定义不太完整,正确的定义是调用不存在或者非public的方法时触发
<?php class User { // method 1 protected function say() { return(true); } // method 2 private function sing() { return(true); } public function __call($name,$args) { $t = func_get_args(); //在默认函数中总用比较明显 print_r($t); } } $m = new User(); $m->say(); //不触发 $m->sing();//触发 $m->talk();//触发 ?>
2.__set,__get,__unset,__isset
有些时候对象为了适应变化,比如Model的字段经常改变,需要零时改变字段,这时需要动态的添加或者删除属性
<?php class PropertyTest { //动态数据存储容器 private $data = array(); /** 重载不能被用在已经定义的属性 */ public $declared = 1; /** 只有从类外部访问这个属性时,重载才会发生 */ private $hidden = 2; public function __set($name, $value) { // $this->$name = $value; //这种也是一种动态扩容的方式,但管理起来比较麻烦 $this->data[$name] = $value; } public function __get($name) { echo "Getting ‘$name‘\n"; if (array_key_exists($name, $this->data)) { return $this->data[$name]; } $trace = debug_backtrace(); trigger_error( ‘Undefined property via __get(): ‘ . $name . ‘ in ‘ . $trace[0][‘file‘] . ‘ on line ‘ . $trace[0][‘line‘], E_USER_NOTICE); return null; } /** PHP 5.1.0之后版本,调用,使用 isset判断时调用 */ public function __isset($name) { echo "Is ‘$name‘ set?\n"; return isset($this->data[$name]); } /** PHP 5.1.0之后版本,删除某个属性时 */ public function __unset($name) { echo "Unsetting ‘$name‘\n"; unset($this->data[$name]); } /** 非魔术方法 */ public function getHidden() { return $this->hidden; } } echo "<pre>\n"; $obj = new PropertyTest; $obj->a = 1; echo $obj->a . "\n\n"; var_dump(isset($obj->a)); unset($obj->a); var_dump(isset($obj->a)); echo "\n"; echo $obj->declared . "\n\n"; echo $obj->getHidden() . "\n"; echo $obj->hidden . "\n"; ?>
代码中如下片段,类似于日志跟踪工具
$trace = debug_backtrace(); trigger_error( ‘Undefined property via __get(): ‘ . $name . ‘ in ‘ . $trace[0][‘file‘] . ‘ on line ‘ . $trace[0][‘line‘], E_USER_NOTICE);
4.get_class, get_parent_class,interface_exists,is_subclass_of,is_a,instanceOf
<?php if(!interface_exists(‘man‘)) { interface man { public function talk($msg); } } class dad { function dad() { // implements some logic } } class child extends dad implements man { public function __toString() { return "I‘m ".get_parent_class($this)."‘s son\n"; } public function talk($msg) { echo $msg; } } class child2 extends dad { public function __toString() { return "I‘m ".get_parent_class(‘child2‘)."‘s son too\n"; } } $foo = new child(); $bar = new child2(); $dad = new dad(); echo ‘<pre>‘; var_dump(get_class($dad)); var_dump(get_class($foo)); var_dump(get_class($bar)); echo "<hr/>"; var_dump(get_parent_class($dad)); var_dump(get_parent_class($foo)); var_dump(get_parent_class($bar)); echo "<hr/>"; var_dump(is_subclass_of($foo,‘dad‘)); var_dump(is_subclass_of($foo,‘child‘)); //child类不是child类的子类,返回false var_dump(is_subclass_of($foo,‘man‘)); echo "<hr/>"; var_dump(is_subclass_of(‘child‘,‘dad‘)); var_dump(is_subclass_of(‘child‘,‘child‘));//child类不是child类的子类.返回false var_dump(is_subclass_of(‘child‘,‘man‘)); echo "<hr/>"; var_dump(is_a($foo,‘dad‘));//is_a等价于 instanceOf var_dump(is_a($foo,‘man‘)); var_dump(is_a($foo,‘child‘)); echo "<hr/>"; var_dump(($foo instanceof dad)); var_dump(($foo instanceof child)); var_dump(($foo instanceof man)); ?>
结果如下
string(3) "dad" string(5) "child" string(6) "child2" ------------------------------------------------------------------- bool(false) string(3) "dad" string(3) "dad" ------------------------------------------------------------------- bool(true) bool(false) bool(true) ------------------------------------------------------------------- bool(true) bool(false) bool(true) ------------------------------------------------------------------- bool(true) bool(true) bool(true) ------------------------------------------------------------------- bool(true) bool(true) bool(true)
好了,今天就到这里
时间: 2024-10-09 22:32:42