1
0
mirror of https://github.com/php/doc-zh.git synced 2026-04-25 17:18:15 +02:00
Files
archived-doc-zh/reference/classobj/reference.xml
T
Krazy Nio 68154591bf Translate comments of example scripts
git-svn-id: https://svn.php.net/repository/phpdoc/zh/trunk@124169 c90b9560-bf6c-de11-be94-00142212c4b1
2003-04-21 14:57:07 +00:00

210 lines
4.7 KiB
XML

<?xml version="1.0" encoding="gb2312"?>
<!-- $Revision: 1.2 $ -->
<!-- $Author: nio $ -->
<!-- EN-Revision: 1.6 Maintainer: nio Status: ready -->
<reference id="ref.classobj">
<title>类/对象函数</title>
<titleabbrev>类/对象</titleabbrev>
<partintro>
<section id="classobj.intro">
&reftitle.intro;
<para>
这些函数允许你获取类和对象实例的信息。你可以取得对象所属的类的名字,以及它的成员属性和方法。通过使用这些函数,你不仅可以弄清楚一个对象类的全体成员,而且可以知道它的起源(例如,该对象类是哪个类的扩展)。
</para>
</section>
<section id="classobj.requirements">
&reftitle.required;
&no.requirement;
</section>
<section id="classobj.installation">
&reftitle.install;
&no.install;
</section>
<section id="classobj.configuration">
&reftitle.runtime;
&no.config;
</section>
<section id="classobj.resources">
&reftitle.resources;
&no.resource;
</section>
<section id="classobj.constants">
&reftitle.constants;
&no.constants;
</section>
<section id="classobj.examples">
&reftitle.examples;
<para>
在这个例子中,我们先定义了一个基础类和一个扩展类。基础类描述的是普通的蔬菜(Vegetable),它是否可食用(is_edible)以及它是什么颜色(what_color)。子类
<varname>Spinach</varname> 增加了“烹调”的方法(cook_it)和“获知它是否已被烹调了”的方法(is_cooked)。
</para>
<para>
<example>
<title>classes.inc</title>
<programlisting role="php">
<![CDATA[
<?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 的结束
?>
]]>
</programlisting>
</example>
</para>
<para>
接着,我们从这些对象中创建了两个对象实例,打印出它们的相关信息,包括它们的起源。我们还定义了一些实用函数,它们主要是为了让变量的输出好看些。
</para>
<para>
<example>
<title>test_script.php</title>
<programlisting role="php">
<![CDATA[
<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>
]]>
</programlisting>
</example>
</para>
<para>
上边例子中重点要注意的是对象
<varname>$leafy</varname>
<classname>Spinach</classname> 类的一个实例,而
<classname>Spinach</classname> 类是
<classname>Vegetable</classname> 类的一个子类,因此上边脚本的最后部分将会输出:
</para>
<para>
<informalexample>
<programlisting>
<![CDATA[
[...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach a subclass of Vegetable
]]>
</programlisting>
</informalexample>
</para>
</section>
</partintro>
&reference.classobj.functions;
</reference>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->