-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathexamples.xml
194 lines (166 loc) · 4.4 KB
/
examples.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?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
-->