How do I know the methods and attributes of a class instance? How do I know the methods and attributes of a class instance? ------ Solution ------------------ example 1. get_class_methods () example & lt ;? Phpclassmyclass {constructorfunctionmyclass () {return (TRUE); how do I know the methods and attributes of a class instance?
How do I know the methods and attributes of a class instance?
------ Solution --------------------
Example 1. get_class_methods () example
Class myclass {
// Constructor
Function myclass (){
Return (TRUE );
}
// Method 1
Function myfunc1 (){
Return (TRUE );
}
// Method 2
Function myfunc2 (){
Return (TRUE );
}
}
$ My_object = new myclass ();
$ Class_methods = get_class_methods (get_class ($ my_object ));
Foreach ($ class_methods as $ method_name ){
Echo "$ method_name \ n ";
}
?>
Running result:
Myclass
Myfunc1
Myfunc2
Example 1. get_class_vars () example
Class myclass {
Var $ var1; // This variable has no default value ......
Var $ var2 = "xyz ";
Var $ var3 = 100;
// Constructor
Function myclass (){
Return (TRUE );
}
}
$ My_class = new myclass ();
$ Class_vars = get_class_vars (get_class ($ my_class ));
Foreach ($ class_vars as $ name => $ value ){
Echo "$ name: $ value \ n ";
}
?>
Running result:
// Before PHP 4.2.0
Var2: xyz
Var3: 100
// Starting with PHP 4.2.0
Var1:
Var2: xyz
Var3: 100
------ Solution --------------------
Search for the php Manual, which contains a reflection concept (reflection)
PHP code
abc; } }//Instantiate the object$b = new a();//Instantiate the reflection object$reflector = new ReflectionClass('a');//Display the object properties$properties = $reflector->getProperties();foreach($properties as $property){ echo "\$b->", $property->getName(), " => ", $b->{$property->getName()}, "\n";}//Display the object methods$methods = $reflector->getMethods();foreach($methods as $method){ echo "\$b->", $method->getName(), " => ", $b->{$method->getName()}(), "\n";}