8

我正在使用 Drupal 6 中的 Views 2,并且很难找到有关 View 对象方法的文档。有没有像 print_r 这样输出方法和字段的 PHP 函数?

4

4 回答 4

35

我相信您正在寻找get_class_methods。如果是这种情况,您可能也会对get_class_vars感兴趣。

于 2009-08-17T20:06:25.847 回答
10

您可能会对反射 API感兴趣(如果它不是矫枉过正的话)。具体来说:-

<?php
    Reflection::export(new ReflectionClass('View'));
?>

查看手册以获取更深入的示例。

于 2009-08-17T20:09:28.303 回答
2

除了 Mathachew 提到的函数之外,您还可以查看Reflection,尤其是ReflectionClass类。

$class = new ReflectionClass('YourViewClass');
$class->getMethods();
$class->getProperties();
于 2009-08-17T20:13:27.437 回答
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 ]

希望你会发现它很有用。问候。

于 2014-09-20T01:04:41.883 回答