reference/classobj/examples.xml
6f11457f11d91834e1240c3351d8c4e289371b6d
...
...
@@ -6,62 +6,66 @@
6
6
<para>
7
7
In this example, we first define a base class and an extension
8
8
of the class. The base class describes a general vegetable,
9
-
whether it is edible or not and what is its color. The subclass
9
+
whether it is edible, and what is its color. The subclass
10
10
<varname>Spinach</varname> adds a method to cook it and another to
11
11
find out if it is cooked.
12
12
</para>
13
13
<para>
14
14
<example>
15
-
<title>classes.inc</title>
15
+
<title>Class Definitions</title>
16
+
<para><varname>Vegetable</varname></para>
16
17
<programlisting role="php">
17
18
<![CDATA[
18
19
<?php
19
20

20
-
// base class with member properties and methods
21
21
class Vegetable {
22
+
public $edible;
22
23

23
-
var $edible;
24
-
var $color;
24
+
public $color;
25
25

26
-
function __construct($edible, $color="green")
27
-
{
28
-
$this->edible = $edible;
29
-
$this->color = $color;
30
-
}
26
+
public function __construct($edible, $color = "green")
27
+
{
28
+
$this->edible = $edible;
29
+
$this->color = $color;
30
+
}
31
31

32
-
function is_edible()
33
-
{
34
-
return $this->edible;
35
-
}
32
+
public function isEdible()
33
+
{
34
+
return $this->edible;
35
+
}
36
36

37
-
function what_color()
38
-
{
39
-
return $this->color;
40
-
}
37
+
public function getColor()
38
+
{
39
+
return $this->color;
40
+
}
41
+
}
41
42

42
-
} // end of class Vegetable
43
+
?>
44
+
]]>
45
+
</programlisting>
46
+
<para><varname>Spinach</varname></para>
47
+
<programlisting role="php">
48
+
<![CDATA[
49
+
<?php
43
50

44
-
// extends the base class
45
51
class Spinach extends Vegetable {
46
-

47
-
var $cooked = false;
48
-

49
-
function __construct()
50
-
{
51
-
parent::__construct(true, "green");
52
-
}
53
-

54
-
function cook_it()
55
-
{
56
-
$this->cooked = true;
57
-
}
58
-

59
-
function is_cooked()
60
-
{
61
-
return $this->cooked;
62
-
}
63
-

64
-
} // end of class Spinach
52
+
public $cooked = false;
53
+

54
+
public function __construct()
55
+
{
56
+
parent::__construct(true, "green");
57
+
}
58
+

59
+
public function cook()
60
+
{
61
+
$this->cooked = true;
62
+
}
63
+

64
+
public function isCooked()
65
+
{
66
+
return $this->cooked;
67
+
}
68
+
}
65
69

66
70
?>
67
71
]]>
...
...
@@ -79,40 +83,37 @@ class Spinach extends Vegetable {
79
83
<title>test_script.php</title>
80
84
<programlisting role="php">
81
85
<![CDATA[
82
-
<pre>
83
86
<?php
84
87

85
-
include "classes.inc";
88
+
// register autoloader to load classes
89
+
spl_autoload_register();
86
90

87
-
// utility functions
88
-

89
-
function print_vars($obj)
91
+
function printProperties($obj)
90
92
{
91
-
foreach (get_object_vars($obj) as $prop => $val) {
92
-
echo "\t$prop = $val\n";
93
-
}
93
+
foreach (get_object_vars($obj) as $prop => $val) {
94
+
echo "\t$prop = $val\n";
95
+
}
94
96
}
95
97

96
-
function print_methods($obj)
98
+
function printMethods($obj)
97
99
{
98
-
$arr = get_class_methods(get_class($obj));
99
-
foreach ($arr as $method) {
100
-
echo "\tfunction $method()\n";
101
-
}
100
+
$arr = get_class_methods(get_class($obj));
101
+
foreach ($arr as $method) {
102
+
echo "\tfunction $method()\n";
103
+
}
102
104
}
103
105

104
-
function class_parentage($obj, $class)
106
+
function objectBelongsTo($obj, $class)
105
107
{
106
-
if (is_subclass_of($GLOBALS[$obj], $class)) {
107
-
echo "Object $obj belongs to class " . get_class($GLOBALS[$obj]);
108
-
echo ", a subclass of $class\n";
109
-
} else {
110
-
echo "Object $obj does not belong to a subclass of $class\n";
111
-
}
108
+
if (is_subclass_of($obj, $class)) {
109
+
echo "Object belongs to class " . get_class($obj);
110
+
echo ", a subclass of $class\n";
111
+
} else {
112
+
echo "Object does not belong to a subclass of $class\n";
113
+
}
112
114
}
113
115

114
116
// instantiate 2 objects
115
-

116
117
$veggie = new Vegetable(true, "blue");
117
118
$leafy = new Spinach();
118
119

...
...
@@ -123,34 +124,47 @@ echo ", PARENT " . get_parent_class($leafy) . "\n";
123
124

124
125
// show veggie properties
125
126
echo "\nveggie: Properties\n";
126
-
print_vars($veggie);
127
+
printProperties($veggie);
127
128

128
129
// and leafy methods
129
130
echo "\nleafy: Methods\n";
130
-
print_methods($leafy);
131
+
printMethods($leafy);
131
132

132
133
echo "\nParentage:\n";
133
-
class_parentage("leafy", "Spinach");
134
-
class_parentage("leafy", "Vegetable");
134
+
objectBelongsTo($leafy, Spinach::class);
135
+
objectBelongsTo($leafy, Vegetable::class);
136
+

135
137
?>
136
-
</pre>
137
138
]]>
138
139
</programlisting>
139
-
<para>
140
-
One important thing to note in the example above is that
141
-
the object <varname>$leafy</varname> is an instance of the class
142
-
<classname>Spinach</classname> which is a subclass of
143
-
<classname>Vegetable</classname>,
144
-
therefore the last part of the script above will output:
145
-
</para>
140
+
&examples.outputs;
146
141
<screen>
147
142
<![CDATA[
148
-
[...]
143
+
veggie: CLASS Vegetable
144
+
leafy: CLASS Spinach, PARENT Vegetable
145
+

146
+
veggie: Properties
147
+
edible = 1
148
+
color = blue
149
+

150
+
leafy: Methods
151
+
function __construct()
152
+
function cook()
153
+
function isCooked()
154
+
function isEdible()
155
+
function getColor()
156
+

149
157
Parentage:
150
-
Object leafy does not belong to a subclass of Spinach
151
-
Object leafy belongs to class spinach, a subclass of Vegetable
158
+
Object does not belong to a subclass of Spinach
159
+
Object belongs to class Spinach, a subclass of Vegetable
152
160
]]>
153
161
</screen>
162
+
<para>
163
+
One important thing to note in the example above is that
164
+
the object <varname>$leafy</varname> is an instance of the class
165
+
<classname>Spinach</classname> which is a subclass of
166
+
<classname>Vegetable</classname>.
167
+
</para>
154
168
</example>
155
169
</para>
156
170
</appendix>
...
...
@@ -175,4 +189,3 @@ vim600: syn=xml fen fdm=syntax fdl=2 si
175
189
vim: et tw=78 syn=sgml
176
190
vi: ts=1 sw=1
177
191
-->
178
-

179
192