mirror of
https://github.com/php/doc-ja.git
synced 2026-03-24 15:12:22 +01:00
195 lines
4.4 KiB
XML
195 lines
4.4 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 6f11457f11d91834e1240c3351d8c4e289371b6d Maintainer: takagi Status: ready -->
|
|
<!-- CREDITS: hirokawa -->
|
|
|
|
|
|
<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";
|
|
|
|
// 変数 veggie のプロパティを表示
|
|
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>Vegetable</classname> のサブクラスであるクラス
|
|
<classname>Spinach</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
|
|
-->
|
|
|