File: reference.xml

package info (click to toggle)
php-doc 20061001-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 45,764 kB
  • ctags: 1,611
  • sloc: xml: 502,485; php: 7,645; cpp: 500; makefile: 297; perl: 161; sh: 141; awk: 28
file content (226 lines) | stat: -rw-r--r-- 5,175 bytes parent folder | download
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.15 $ -->
<!-- Purpose: basic.vartype -->
<!-- Membership: core -->

 <reference id="ref.classobj">
  <title>Class/Object Functions</title>
  <titleabbrev>Classes/Objects</titleabbrev>

  <partintro>
    <section id="classobj.intro">
     &reftitle.intro;
     <para>
      These functions allow you to obtain information about classes
      and instance objects. You can obtain the name of the class to
      which an object belongs, as well as its member properties and 
      methods. Using these functions, you can find out not only the
      class membership of an object, but also its parentage (i.e.
      what class is the object class extending).
     </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>
      In this example, we first define a base class and an extension
      of the class. The base class describes a general vegetable,
      whether it is edible or not and what is its color. The subclass
      <varname>Spinach</varname> adds a method to cook it and another to
      find out if it is cooked.
     </para>
     <para>
      <example>
       <title>classes.inc</title>
       <programlisting role="php">
<![CDATA[
<?php

// base class with member properties and methods
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;
    }
    
} // end of class Vegetable

// extends the base class
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;
    }
    
} // end of class Spinach

?>
]]>
       </programlisting>
      </example>
     </para>
     <para>
     We then instantiate 2 objects from these classes and print out
     information about them, including their class parentage.
     We also define some utility functions, mainly to have a nice printout
     of the variables.
     </para>
     <para>
      <example>
       <title>test_script.php</title>
       <programlisting role="php">
<![CDATA[
<pre>
<?php

include "classes.inc";

// utility functions

function print_vars($obj) 
{
    foreach (get_object_vars($obj) as $prop => $val) {
        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) 
{
    if (is_subclass_of($GLOBALS[$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";
    }
}

// instantiate 2 objects

$veggie = new Vegetable(true, "blue");
$leafy = new Spinach();

// print out information about objects
echo "veggie: CLASS " . get_class($veggie) . "\n";
echo "leafy: CLASS " . get_class($leafy);
echo ", PARENT " . get_parent_class($leafy) . "\n";

// show veggie properties
echo "\nveggie: Properties\n";
print_vars($veggie);

// and leafy methods
echo "\nleafy: Methods\n";
print_methods($leafy);

echo "\nParentage:\n";
class_parentage("leafy", "Spinach");
class_parentage("leafy", "Vegetable");
?>
</pre>
]]>
       </programlisting>
       <para>
        One important thing to note in the example above is that
        the object <varname>$leafy</varname> is an instance of the class
        <classname>Spinach</classname> which is a subclass of 
        <classname>Vegetable</classname>,
        therefore the last part of the script above will output:
       </para>
       <screen>
<![CDATA[
       [...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach a subclass of Vegetable
]]>
       </screen>
      </example>
     </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
-->