今天在学习PHP的时候看到两个非常有用的属性:__toString和ReflectionClass,下面将它的用法贴出来,供大家参考。
其中ReflectionClass是非常有用的,如果在使用一个不知道的类的时候,利用这个方法就可以将类的构造输出到屏幕上
1
<?php
2
class Page{
3
// class Page's attributes
4
public $content;
5
public $title = '这是我的TITLE';
6
public $keywords = '这是我的keywords';
7
public $buttons = array(
8
'Home' => 'home.php',
9
'Contact' => 'contact.php',
10
'Site Map' => 'map.php'
11
);
12
public function __toString(){
13
// var_export 打印出类中的所有属性值
14
return (var_export($this,TRUE));
15
}
16
public function _set($name,$value){
17
$this->$name = $value;
18
}
19
20
public function display(){
21
echo '<html><br/><head><br/>';
22
}
23
}
24
$test = new Page();
25
echo $test.'<br/>';
26
// ReflectionClass 为反射属性 显示关于类的信息
27
$class = new ReflectionClass('Page');
28
echo '<pre>';
29
echo $class;
30
echo '</pre>';
31
?>
<?php2
class Page{3
// class Page's attributes4
public $content;5
public $title = '这是我的TITLE';6
public $keywords = '这是我的keywords';7
public $buttons = array(8
'Home' => 'home.php',9
'Contact' => 'contact.php',10
'Site Map' => 'map.php'11
);12
public function __toString(){13
// var_export 打印出类中的所有属性值14
return (var_export($this,TRUE));15
}16
public function _set($name,$value){17
$this->$name = $value;18
}19

20
public function display(){21
echo '<html><br/><head><br/>';22
}23
}24
$test = new Page();25
echo $test.'<br/>';26
// ReflectionClass 为反射属性 显示关于类的信息27
$class = new ReflectionClass('Page');28
echo '<pre>';29
echo $class;30
echo '</pre>';31
?>
浙公网安备 33010602011771号