How do I know the methods and attributes of a class instance?

Source: Internet
Author: User
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";} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.