mirror of
https://github.com/php/doc-zh.git
synced 2026-03-24 15:12:20 +01:00
187 lines
4.0 KiB
XML
187 lines
4.0 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 6f11457f11d91834e1240c3351d8c4e289371b6d Maintainer: daijie Status: ready -->
|
|
<!-- CREDITS: mowangjuanzi -->
|
|
<appendix xml:id="classobj.examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
在这个示例中,首先定义了一个基类和该类的扩展。基类描述了普通的蔬菜,
|
|
是否可食用及其颜色。子类 <varname>Spinach</varname>
|
|
添加了烹饪的方法和检查是否已烹饪的方法。
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>定义类</title>
|
|
<para><varname>Vegetable</varname></para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class Vegetable {
|
|
public $edible;
|
|
|
|
public $color;
|
|
|
|
public function __construct($edible, $color = "green")
|
|
{
|
|
$this->edible = $edible;
|
|
$this->color = $color;
|
|
}
|
|
|
|
public function isEdible()
|
|
{
|
|
return $this->edible;
|
|
}
|
|
|
|
public function getColor()
|
|
{
|
|
return $this->color;
|
|
}
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para><varname>Spinach</varname></para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class Spinach extends Vegetable {
|
|
public $cooked = false;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct(true, "green");
|
|
}
|
|
|
|
public function cook()
|
|
{
|
|
$this->cooked = true;
|
|
}
|
|
|
|
public function isCooked()
|
|
{
|
|
return $this->cooked;
|
|
}
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
接下来从这些类中实例化了两个对象并打印了它们的信息,包括了他们类的继承关系。
|
|
同时也定义了一些实用函数,主要为了漂亮地打印出这些变量。
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>test_script.php</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
// 注册自动加载器以加载类
|
|
spl_autoload_register();
|
|
|
|
function printProperties($obj)
|
|
{
|
|
foreach (get_object_vars($obj) as $prop => $val) {
|
|
echo "\t$prop = $val\n";
|
|
}
|
|
}
|
|
|
|
function printMethods($obj)
|
|
{
|
|
$arr = get_class_methods(get_class($obj));
|
|
foreach ($arr as $method) {
|
|
echo "\tfunction $method()\n";
|
|
}
|
|
}
|
|
|
|
function objectBelongsTo($obj, $class)
|
|
{
|
|
if (is_subclass_of($obj, $class)) {
|
|
echo "Object belongs to class " . get_class($obj);
|
|
echo ", a subclass of $class\n";
|
|
} else {
|
|
echo "Object does not belong to a subclass of $class\n";
|
|
}
|
|
}
|
|
|
|
// 实例化 2 对象
|
|
$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";
|
|
|
|
// 显示蔬菜的属性
|
|
echo "\nveggie: Properties\n";
|
|
printProperties($veggie);
|
|
|
|
// 和 leafy 的方法
|
|
echo "\nleafy: Methods\n";
|
|
printMethods($leafy);
|
|
|
|
echo "\nParentage:\n";
|
|
objectBelongsTo($leafy, Spinach::class);
|
|
objectBelongsTo($leafy, Vegetable::class);
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&examples.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
veggie: CLASS Vegetable
|
|
leafy: CLASS Spinach, PARENT Vegetable
|
|
|
|
veggie: Properties
|
|
edible = 1
|
|
color = blue
|
|
|
|
leafy: Methods
|
|
function __construct()
|
|
function cook()
|
|
function isCooked()
|
|
function isEdible()
|
|
function getColor()
|
|
|
|
Parentage:
|
|
Object does not belong to a subclass of Spinach
|
|
Object belongs to class Spinach, a subclass of Vegetable
|
|
]]>
|
|
</screen>
|
|
<para>
|
|
一个重要的事情是需要注意在上面的示例中,对象 <varname>$leafy</varname> 是
|
|
<classname>Spinach</classname> 的实例(<classname>Vegetable</classname> 的子类)。
|
|
</para>
|
|
</example>
|
|
</para>
|
|
</appendix>
|
|
|
|
<!-- 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:"~/.phpdoc/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
|
|
-->
|