Home >php教程 >PHP视频 >Excerpted from PHP manual [3] – Classes/Objects class/object functions

Excerpted from PHP manual [3] – Classes/Objects class/object functions

黄舟
黄舟Original
2016-12-22 10:11:001568browse

Introduction: This class of functions allows obtaining information about classes and object instances. You can get the name of the class to which the object belongs, as well as its member properties and methods. By using these functions, you can not only figure out all the members of an object class, but also know its origin (for example, which class the object class is an extension of).

class_exists — Check if the class is defined




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

This function returns TRUE if the class pointed to by class_name is already defined, otherwise it returns FALSE.
class_exists() will try to call __autoload by default. If you don’t want class_exists() to call __autoload, you can set the autoload parameter to FALSE.

get_class_methods — Returns an array consisting of the method names of the class




array get_class_methods ( mixed $class_name )

Starting from PHP 4.0.6, you can specify the object itself instead of class_name, that is:




$class_methods = get_class_methods('myclass');
// or
$class_methods = get_class_methods(new myclass());


Since PHP 5, this function returns the name of the method as it is defined (case sensitive) . In PHP 4 always returns lowercase.

get_class_vars — Returns an array composed of the default properties of the class




array get_class_vars ( string $class_name )


Returns an associative array composed of the default public properties of the class. The elements of this array are varname => ; exists in the form of value.

get_class — Returns the class name of the object




string get_class ([ object $obj ] )


Returns the name of the class to which the object instance obj belongs. Returns FALSE if obj is not an object.

get_declared_classes — Returns an array of names of defined classes




array get_declared_classes ( void )


get_declared_interfaces — Returns an array containing all declared interfaces




array get_declared_interfaces ( void )


get_object_vars — Returns an associative array composed of object properties




array get_object_vars ( object $obj )


get_parent_class — Returns the parent class name of an object or class




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, return the parent class name of the class named by this string. This feature was added in PHP 4.0.5.
Since PHP 5, obj is optional if called within a method of an object.

interface_exists — Check if the interface has been defined




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


This function returns TRUE if the interface given by interface_name is defined, otherwise it returns FALSE.

is_a — Returns TRUE if the object belongs to this class or this class is the parent class of this object




bool is_a ( object $object , string $class_name )


【Tianya Note】This in PHP 5 The function has been deprecated and replaced by instanceof, the only type operator in PHP. The usage method is as follows:




class A { }
class B { }
$thing = new A;
if ( $thing instanceof A) {
echo 'A';
}
if ($thing instanceof B) {
echo 'B';
}
?>


is_subclass_of — if this object is a subclass of this class class, return TRUE




bool is_subclass_of ( object $object , string $class_name )


method_exists — Check whether the method of the class exists




bool method_exists ( object $object , string $meth od_name )


property_exists — Check whether the object or class has the property




bool property_exists ( mixed $class , string $property )


【Tianya Note】It should be noted that if it is in the 'current scope ', if the attribute cannot be accessed, such as private, it will still return FALSE, for example:




class myClass {
public $mine;
private $xpto;
 
static function test() {
// true, it can be accessed from here
var_dump(property_exists('myClass', 'xpto'));
}
}
var_dump(property_exists('myClass', 'mine')); //true
var_dump(property_exists(new myClass, 'mine')); //true
var_dump(property_exists('myClass', 'xpto')); //false, isn't public
myClass::test();
?>

以上就是摘自PHP手册[3] – Classes/Objects 类/对象函数的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more