From PHP Manual [3]-Classes/Objects Classes/object functions

Source: Internet
Author: User
Introduction: This class function allows you to obtain information about classes and object instances. You can obtain the name of the class to which the object belongs, as well as its member attributes and methods. By using these functions, you can not only understand all the members of an object class, but also know its origins (for example, the extension of the class of this object class ).

Introduction: This class function allows you to obtain information about classes and object instances. You can obtain the name of the class to which the object belongs, as well as its member attributes and methods. By using these functions, you can not only understand all the members of an object class, but also know its origins (for example, the extension of the class of this object class ).

Class_exists-check whether the class is defined

 
 
  1. bool class_exists ( string $class_name [, bool $autoload ] )

If the class referred to by class_name has been defined, this function returns TRUE; otherwise, FALSE.
By default, class_exists () will attempt to call _ autoload. if you do not want class_exists () to call _ autoload, you can set the autoload parameter to FALSE.

Get_class_methods-returns an array composed of class method names.

 
 
  1. array get_class_methods ( mixed $class_name )

From PHP 4.0.6, you can specify the object itself to replace class_name, that is:

 
 
  1. $class_methods = get_class_methods('myclass');
  2. // or
  3. $class_methods = get_class_methods(new myclass());

Since PHP 5, this function is returned (case sensitive) according to the name defined by the method ). PHP 4 always returns lowercase letters.

Get_class_vars-returns an array composed of the default attributes of the class.

 
 
  1. array get_class_vars ( string $class_name )

Returns an associated array composed of the default public attributes of the class. the elements of this array exist in the form of varname => value.

Get_class-class name of the returned object

 
 
  1. string get_class ([ object $obj ] )

Returns the name of the class to which the object (obj) belongs. If obj is not an object, FALSE is returned.

Get_declared_classes-returns an array composed of the names of defined classes.

 
 
  1. array get_declared_classes ( void )

Get_declared_interfaces-returns an array containing all declared interfaces.

 
 
  1. array get_declared_interfaces ( void )

Get_object_vars-returns an associated array composed of object attributes.

 
 
  1. array get_object_vars ( object $obj )

Get_parent_class-return the parent class name of the object or class

 
 
  1. string get_parent_class ([ mixed $obj ] )

If obj is an object, return the parent class name of the class to which the object instance obj belongs.
If obj is a string, the parent class name of the class named in this string is returned. This function is added in PHP 4.0.5.
From PHP 5, if called in the object method, obj is optional.

Interface_exists-check whether the interface has been defined

 
 
  1. bool interface_exists ( string $interface_name [, bool $autoload ] )

This function returns TRUE if the interface specified by interface_name has been defined. otherwise, FALSE is returned.

Is_a-if the object belongs to this class or the class is the parent class of this object, TRUE is returned.

 
 
  1. bool is_a ( object $object , string $class_name )

[Tianya note] This function has been deprecated in PHP 5 and replaced by the unique type operator instanceof in PHP. the usage is as follows:

 
 
  1. class A { }
  2. class B { }
  3. $thing = new A;
  4. if ($thing instanceof A) {
  5. echo 'A';
  6. }
  7. if ($thing instanceof B) {
  8. echo 'B';
  9. }
  10. ?>

Is_subclass_of-if this object is a subclass of this class, TRUE is returned.

 
 
  1. bool is_subclass_of ( object $object , string $class_name )

Method_exists-check whether the class method exists

 
 
  1. bool method_exists ( object $object , string $method_name )

Property_exists-check whether the object or class has this attribute

 
 
  1. bool property_exists ( mixed $class , string $property )

[Tianya note] it should be noted that if the attribute cannot be accessed within 'current range', for example, private, FALSE is still returned, for example:

 
 
  1. class myClass {
  2. public $mine;
  3. private $xpto;
  4.  
  5. static function test() {
  6. // true, it can be accessed from here
  7. var_dump(property_exists('myClass', 'xpto'));
  8. }
  9. }
  10. var_dump(property_exists('myClass', 'mine')); //true
  11. var_dump(property_exists(new myClass, 'mine')); //true
  12. var_dump(property_exists('myClass', 'xpto')); //false, isn't public
  13. myClass::test();
  14. ?>

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.