PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<classkit_method_renamecall_user_method_array>
Last updated: Mon, 16 Jul 2012

XI. 类/对象函数库

简介

这些函数允许你获取类和对象实例的信息。你可以取得对象所属的类的名字,以及它的成员属性和方法。通过使用这些函数,你不仅可以弄清楚一个对象类的全体成员,而且可以知道它的起源(例如,该对象类是哪个类的扩展)。

需求

无需外部库文件就可以加入本扩展模块的支持。

安装

这些函数作为 PHP 核心的一部分,无需被安装即可使用。

运行时配置

本扩展模块在 php.ini 中未定义任何设置指令。

资源类型

本扩展模块未定义任何资源类型。

预定义常量

本扩展模块未定义任何常量。

在这个例子中,我们先定义了一个基础类和一个扩展类。基础类描述的是普通的蔬菜(Vegetable),它是否可食用(is_edible)以及它是什么颜色(what_color)。子类 Spinach 增加了“烹调”的方法(cook_it)和“获知它是否已被烹调了”的方法(is_cooked)。

例子 1. classes.inc

<?php

// 基础类及其属性和方法
class Vegetable {

   var
$edible;
   var
$color;

   function
Vegetable( $edible, $color="green" ) {
      
$this->edible = $edible;
      
$this->color = $color;
   }

   function
is_edible() {
       return
$this->edible;
   }

   function
what_color() {
       return
$this->color;
   }

}
// 类 Vegetable 的结束

// 从基础类扩展
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;
   }

}
// 类 Spinach 的结束

?>

接着,我们从这些对象中创建了两个对象实例,打印出它们的相关信息,包括它们的起源。我们还定义了一些实用函数,它们主要是为了让变量的输出好看些。

例子 2. test_script.php

<pre>
<?php

include "classes.inc";

// 实用函数

function print_vars($obj) {
  
$arr = get_object_vars($obj);
   while (list(
$prop, $val) = each($arr))
       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) {
   global $
$obj;
   if (
is_subclass_of($$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 subclass of $class\n";
   }
}

// 创建两个对象实例

$veggie = new Vegetable(true,"blue");
$leafy = new Spinach();

// 打印出对象的相关信息
echo "veggie: CLASS ".get_class($veggie)."\n";
echo
"leafy: CLASS ".get_class($leafy);
echo
", PARENT ".get_parent_class($leafy)."\n";

// 显示 veggie 的属性
echo "\nveggie: Properties\n";
print_vars($veggie);

// 显示 leafy 的方法
echo "\nleafy: Methods\n";
print_methods($leafy);

echo
"\nParentage:\n";
class_parentage("leafy", "Spinach");
class_parentage("leafy", "Vegetable");
?>
</pre>

上边例子中重点要注意的是对象 $leafySpinach 类的一个实例,而 Spinach 类是 Vegetable 类的一个子类,因此上边脚本的最后部分将会输出:

[...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach a subclass of Vegetable

目录
call_user_method_array --  调用一个用户方法,同时传递参数数组[已停用]
call_user_method --  调用特定对象的用户方法[已停用]
class_exists -- 检查类是否已定义
get_class_methods -- 返回由类的方法名组成的数组
get_class_vars --  返回由类的默认属性组成的数组
get_class -- 返回对象的类名
get_declared_classes -- 返回由已定义类的名字所组成的数组
get_declared_interfaces --  Returns an array of all declared interfaces
get_object_vars -- 返回由对象属性组成的关联数组
get_parent_class -- 返回对象或类的父类名
interface_exists -- Checks if the interface has been defined
is_a --  如果对象属于该类或该类是此对象的父类则返回 TRUE
is_subclass_of --  如果此对象是该类的子类,则返回 TRUE
method_exists -- 检查类的方法是否存在
property_exists --  Checks if the object or class has a property



<classkit_method_renamecall_user_method_array>
 Last updated: Mon, 16 Jul 2012
 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://manual.phpv.net/
Last updated: Thu Jul 7 19:13:47 2005 CST