263企业邮箱—中国企业邮箱第一品牌

263企业邮箱申请热线: 800-820-1263
企业邮箱首页 | 产品优势 | 成功客户 | 服务说明 | 解决方案 | 产品价格 | 如何购买 | 功能演示 | 在线帮助
    

中小企业发展推动企业邮箱服务步...

组合拳威力初显 263企业邮箱...

263企业邮箱推出263G邮箱...

outlook错误(0x800...

南火科技深切慰问汶川地震灾区同...

企业邮箱运营商服务沪企屡出“绝...

超大邮箱空间

智能反垃圾系统

超大附件
南北电信互通保障
海外转发
服务品质保障

企业邮箱用户使用手册

企业邮箱用户使用手册PDF文件下载

企业邮箱管理员使用手册

企业邮箱服务品质保障协议(SLA)
Outlook Express 错误代码表
基本测试命令
退信的常见问题及解决方法
PHP手册
企业邮件监控跟踪方案
防范垃圾邮件解决方案
海外企业邮件发送方案
南北邮件问题解决方案
超大企业邮箱空间方案
超大附件发送解决方案
邮件查杀病毒解决方案
 

PHP 手册

XII. Classes/Objects 类/对象函数

简介

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

需求

要编译本扩展模块不需要外部库文件。

安装

本函数库作为 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";

// utility functions

function print_vars($obj)
{
foreach (get_object_vars($obj) as $prop => $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 subclass of $class\n";
}
}

// instantiate 2 objects

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

// print out information about objects
echo "veggie: CLASS " . get_class($veggie) . "\n";
echo "leafy: CLASS " . get_class($leafy);
echo ", PARENT " . get_parent_class($leafy) . "\n";

// show veggie properties
echo "\nveggie: Properties\n";
print_vars($veggie);

// and leafy methods
echo "\nleafy: Methods\n";
print_methods($leafy);

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

上边例子中重点要注意的是对象 $leafy 是 Spinach 类的一个实例,而 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

 

  

后退      起点      前进


关于我们 | 联系我们 | 如何交费 | 招聘信息 | 合作伙伴 | 友情链接 | 版权声明 | 隐私保护 | 新闻中心 | 网站地图 | 企业邮箱博客
263企业邮局—中国企业邮局第一品牌 Copyright© 1998-2006  263企业邮箱版权所有 All rights reserved.
电话: 021-51099455  800-820-1263 (上海) 传真: 021-51062789