我正在使用Drupal 6中的Views 2,我很难找到有关View对象方法的文档。是否有像print_r这样的PHP函数输出方法和字段?
答案 0 :(得分:34)
我相信你正在寻找get_class_methods。如果是这种情况,get_class_vars也可能会让您感兴趣。
答案 1 :(得分:10)
Reflection API可能会引起您的兴趣(如果它不是过度杀伤)。具体做法是: -
<?php
Reflection::export(new ReflectionClass('View'));
?>
查看手册以获得更深入的示例。
答案 2 :(得分:1)
除了Mathachew提到的功能之外,您还可以查看Reflection,尤其是ReflectionClass
类。
$class = new ReflectionClass('YourViewClass');
$class->getMethods();
$class->getProperties();
答案 3 :(得分:1)
我编写了这个简单的函数,它不仅显示给定对象的方法,还显示其属性,封装和一些其他有用的信息,如发布说明(如果给出)。
function TO($object){ //Test Object
if(!is_object($object)){
throw new Exception("This is not a Object");
return;
}
if(class_exists(get_class($object), true)) echo "<pre>CLASS NAME = ".get_class($object);
$reflection = new ReflectionClass(get_class($object));
echo "<br />";
echo $reflection->getDocComment();
echo "<br />";
$metody = $reflection->getMethods();
foreach($metody as $key => $value){
echo "<br />". $value;
}
echo "<br />";
$vars = $reflection->getProperties();
foreach($vars as $key => $value){
echo "<br />". $value;
}
echo "</pre>";
}
为了向您展示它是如何工作的,我创建了一些随机的示例类。让我们创建一个名为Person的类,并在类声明的上方放置一些发行说明:
/**
* DocNotes - This is description of this class if given else it will display false
*/
class Person{
private $name;
private $dob;
private $height;
private $weight;
private static $num;
function __construct($dbo, $height, $weight, $name) {
$this->dob = $dbo;
$this->height = (integer)$height;
$this->weight = (integer)$weight;
$this->name = $name;
self::$num++;
}
public function eat($var="", $sar=""){
echo $var;
}
public function potrzeba($var =""){
return $var;
}
}
现在让我们创建一个Person的实例并用我们的函数包装它。
$Wictor = new Person("27.04.1987", 170, 70, "Wictor");
TO($Wictor);
这将输出有关类名,参数和方法的信息,包括封装信息,每个方法的参数编号和名称,方法位置以及存在的代码行。请参阅以下输出:
CLASS NAME = Person
/**
* DocNotes - This is description of this class if given else it will display false
*/
Method [ public method __construct ] {
@@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 75 - 82
- Parameters [4] {
Parameter #0 [ $dbo ]
Parameter #1 [ $height ]
Parameter #2 [ $weight ]
Parameter #3 [ $name ]
}
}
Method [ public method eat ] {
@@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 83 - 85
- Parameters [2] {
Parameter #0 [ $var = '' ]
Parameter #1 [ $sar = '' ]
}
}
Method [ public method potrzeba ] {
@@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 86 - 88
- Parameters [1] {
Parameter #0 [ $var = '' ]
}
}
Property [ private $name ]
Property [ private $dob ]
Property [ private $height ]
Property [ private $weight ]
Property [ private static $num ]
希望你会发现它很有用。问候。