PHP variables and type extensions and objects
I. Overview and installation
These functions allow you to obtain information about classes and object instances. You can obtain the class name of an object, or its member attributes and methods. By using these functions, you can not only find the relationship between objects and classes, but also the inheritance relationship between them (for example, which class the object class inherits from ).
For more information about how to implement and use objects and classes in PHP, see the object-oriented chapter.
You do not need to install these functions. they are part of the PHP core.
II. category and object functions
_ Autoload-try to load undefined classes
Call_user_method_array-call a user method and pass the parameter array (obsolete)
Call_user_method-call the user method for a specific object (obsolete)
Class_alias-create an alias for a class
Class_exists-check whether the class is defined
Get_called_class-name of the later Static Binding ("Late Static Binding") class
Get_class_methods-returns an array composed of class method names.
Get_class_vars-returns an array composed of the default attributes of the class.
Get_class-class name of the returned object
Get_declared_classes-returns an array composed of the names of defined classes.
Get_declared_interfaces-returns an array containing all declared interfaces.
Get_declared_traits-returns an array of all defined traits.
Get_object_vars-returns an associated array composed of object attributes.
Get_parent_class-return the parent class name of the object or class
Interface_exists-check whether the interface has been defined
Is_a-if the object belongs to this class or the class is the parent class of this object, TRUE is returned.
Is_subclass_of-if this object is a subclass of this class, TRUE is returned.
Method_exists-check whether the class method exists
Property_exists-check whether the object or class has this attribute
Trait_exists-check whether the specified trait exists
III. Examples
In this example, we first define a base class and its extension. This base class describes common vegetables about whether they are edible and their colors. The subclass Spinach adds the cooking method and another method to check whether cooking is done.
Example #1 classes. inc
edible = $edible; $this->color = $color; } function is_edible() { return $this->edible; } function what_color() { return $this->color; } } // end of class Vegetable // extends the base class class Spinach extends Vegetable { var $cooked = false; function Spinach() { $this->Vegetable(true, "green"); } function cook_it() { $this->cooked = true; } function is_cooked() { return $this->cooked; } } // end of class Spinach?>
Next we instantiate two objects from these classes and print their information, including the inheritance relationships of their classes. At the same time, we have set some practical functions, mainly to print these variables beautifully.
Example #2 test_script.php
$ Val) {echo "\ t $ prop = $ val \ n" ;}} function print_methods ($ obj) {$ arr = get_class_methods (get_class ($ obj )); foreach ($ arr as $ method) {echo "\ tfunction $ method () \ n" ;}} function class_parentage ($ obj, $ class) {if (is_subclass_of ($ GLOBALS [$ obj], $ class) {echo "Object $ obj belongs to class ". get_class ($ obj); echo "a subclass of $ class \ n";} else {echo "Object $ obj does not belong to a sub Class of $ class \ n ";}}// instantiate 2 object $ veggie = new Vegetable (true," blue "); $ leafy = new Spinach (); // Print the information of these objects echo "veggie: CLASS ". get_class ($ veggie ). "\ n"; echo "leafy: CLASS ". get_class ($ leafy); echo ", PARENT ". get_parent_class ($ leafy ). "\ n"; // display the attributes of vegetables echo "\ nveggie: Properties \ n"; print_vars ($ veggie); // and leafy methods echo "\ nleafy: methods \ n "; print_methods ($ leafy); echo" \ nP Arentage: \ n "; class_parentage (" leafy "," Spinach "); class_parentage (" leafy "," Vegetable ");?>
One important thing is that in the above example, the object $ leafy is a Spinach instance (a subclass of Vegetable), and the last part of the script will output the following information:
[...]Parentage:Object leafy does not belong to a subclass of SpinachObject leafy belongs to class spinach a subclass of Vegetable