print_r per ottenere metodi degli oggetti in PHP?
-
18-09-2019 - |
Domanda
Sto lavorando con Vista 2 in Drupal 6, e sto avendo difficoltà a trovare la documentazione sui metodi dell'oggetto View. C'è qualche funzione PHP come print_r che emette i metodi e campi?
Soluzione
Credo che stai cercando get_class_methods . Se questo è il caso, get_class_vars Potrebbe anche interessarti .
Altri suggerimenti
Il API Reflection potrebbe essere di vostro interesse (se non è eccessivo). In particolare: -
<?php
Reflection::export(new ReflectionClass('View'));
?>
Controlla la manuale per maggiori esempi di approfondimento.
Oltre alle funzioni di cui parla Mathachew si può anche dare un'occhiata a Riflessione , in particolare la classe ReflectionClass
.
$class = new ReflectionClass('YourViewClass');
$class->getMethods();
$class->getProperties();
Ho scritto questa semplice funzione che non solo mostra le modalità di un determinato oggetto, ma mostra anche le sue proprietà, incapsulamento e alcune altre informazioni utili come note di rilascio se dato.
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>";
}
Per mostrare come funziona ho creato un po 'di classe esempio a caso. Consente di creare una classe denominata Persona e posto alcune note di rilascio appena sopra la dichiarazione di classe:
/**
* 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;
}
}
Ora ti permette di creare un'istanza di una persona e avvolgere con la nostra funzione.
$Wictor = new Person("27.04.1987", 170, 70, "Wictor");
TO($Wictor);
Questa informazione uscita volontà circa il nome della classe, parametri e metodi, tra cui informazioni incapsulamento e numero ei nomi dei parametri per ciascun metodo, posizione metodo e le righe di codice in cui esiste. Vedere l'output di seguito:
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 ]
Spero che troverete utile. Saluti.