1 | <HTML
|
---|
2 | ><HEAD
|
---|
3 | ><TITLE
|
---|
4 | >::</TITLE
|
---|
5 | ><META
|
---|
6 | NAME="GENERATOR"
|
---|
7 | CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
|
---|
8 | REL="HOME"
|
---|
9 | TITLE="Manuál PHP"
|
---|
10 | HREF="index.html"><LINK
|
---|
11 | REL="UP"
|
---|
12 | TITLE="Classes and Objects"
|
---|
13 | HREF="language.oop.html"><LINK
|
---|
14 | REL="PREVIOUS"
|
---|
15 | TITLE="Constructors"
|
---|
16 | HREF="language.oop.constructor.html"><LINK
|
---|
17 | REL="NEXT"
|
---|
18 | TITLE="parent"
|
---|
19 | HREF="keyword.parent.html"><META
|
---|
20 | HTTP-EQUIV="Content-type"
|
---|
21 | CONTENT="text/html; charset=ISO-8859-2"></HEAD
|
---|
22 | ><BODY
|
---|
23 | CLASS="sect1"
|
---|
24 | BGCOLOR="#FFFFFF"
|
---|
25 | TEXT="#000000"
|
---|
26 | LINK="#0000FF"
|
---|
27 | VLINK="#840084"
|
---|
28 | ALINK="#0000FF"
|
---|
29 | ><DIV
|
---|
30 | CLASS="NAVHEADER"
|
---|
31 | ><TABLE
|
---|
32 | SUMMARY="Header navigation table"
|
---|
33 | WIDTH="100%"
|
---|
34 | BORDER="0"
|
---|
35 | CELLPADDING="0"
|
---|
36 | CELLSPACING="0"
|
---|
37 | ><TR
|
---|
38 | ><TH
|
---|
39 | COLSPAN="3"
|
---|
40 | ALIGN="center"
|
---|
41 | >Manuál PHP</TH
|
---|
42 | ></TR
|
---|
43 | ><TR
|
---|
44 | ><TD
|
---|
45 | WIDTH="10%"
|
---|
46 | ALIGN="left"
|
---|
47 | VALIGN="bottom"
|
---|
48 | ><A
|
---|
49 | HREF="language.oop.constructor.html"
|
---|
50 | ACCESSKEY="P"
|
---|
51 | >Pøedcházející</A
|
---|
52 | ></TD
|
---|
53 | ><TD
|
---|
54 | WIDTH="80%"
|
---|
55 | ALIGN="center"
|
---|
56 | VALIGN="bottom"
|
---|
57 | >Kapitola 14. Classes and Objects</TD
|
---|
58 | ><TD
|
---|
59 | WIDTH="10%"
|
---|
60 | ALIGN="right"
|
---|
61 | VALIGN="bottom"
|
---|
62 | ><A
|
---|
63 | HREF="keyword.parent.html"
|
---|
64 | ACCESSKEY="N"
|
---|
65 | >Dal¹í</A
|
---|
66 | ></TD
|
---|
67 | ></TR
|
---|
68 | ></TABLE
|
---|
69 | ><HR
|
---|
70 | ALIGN="LEFT"
|
---|
71 | WIDTH="100%"></DIV
|
---|
72 | ><DIV
|
---|
73 | CLASS="sect1"
|
---|
74 | ><H1
|
---|
75 | CLASS="sect1"
|
---|
76 | ><A
|
---|
77 | NAME="keyword.paamayim-nekudotayim"
|
---|
78 | ></A
|
---|
79 | ><TT
|
---|
80 | CLASS="literal"
|
---|
81 | >::</TT
|
---|
82 | ></H1
|
---|
83 | ><DIV
|
---|
84 | CLASS="caution"
|
---|
85 | ><P
|
---|
86 | ></P
|
---|
87 | ><TABLE
|
---|
88 | CLASS="caution"
|
---|
89 | BORDER="1"
|
---|
90 | WIDTH="100%"
|
---|
91 | ><TR
|
---|
92 | ><TD
|
---|
93 | ALIGN="CENTER"
|
---|
94 | ><B
|
---|
95 | >Výstraha</B
|
---|
96 | ></TD
|
---|
97 | ></TR
|
---|
98 | ><TR
|
---|
99 | ><TD
|
---|
100 | ALIGN="LEFT"
|
---|
101 | ><P
|
---|
102 | > The following is valid for PHP 4 only.
|
---|
103 | </P
|
---|
104 | ></TD
|
---|
105 | ></TR
|
---|
106 | ></TABLE
|
---|
107 | ></DIV
|
---|
108 | ><P
|
---|
109 | > Sometimes it is useful to refer to functions and variables
|
---|
110 | in base classes or to refer to functions in classes that
|
---|
111 | have not yet any instances. The :: operator is being used
|
---|
112 | for this.
|
---|
113 | </P
|
---|
114 | ><DIV
|
---|
115 | CLASS="informalexample"
|
---|
116 | ><A
|
---|
117 | NAME="AEN5737"
|
---|
118 | ></A
|
---|
119 | ><P
|
---|
120 | ></P
|
---|
121 | ><TABLE
|
---|
122 | BORDER="0"
|
---|
123 | BGCOLOR="#E0E0E0"
|
---|
124 | CELLPADDING="5"
|
---|
125 | ><TR
|
---|
126 | ><TD
|
---|
127 | ><PRE
|
---|
128 | CLASS="php"
|
---|
129 | >class A
|
---|
130 | {
|
---|
131 | function example()
|
---|
132 | {
|
---|
133 | echo "I am the original function A::example().<br>\n";
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | class B extends A
|
---|
138 | {
|
---|
139 | function example()
|
---|
140 | {
|
---|
141 | echo "I am the redefined function B::example().<br>\n";
|
---|
142 | A::example();
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | // there is no object of class A.
|
---|
147 | // this will print
|
---|
148 | // I am the original function A::example().<br>
|
---|
149 | A::example();
|
---|
150 |
|
---|
151 | // create an object of class B.
|
---|
152 | $b = new B;
|
---|
153 |
|
---|
154 | // this will print
|
---|
155 | // I am the redefined function B::example().<br>
|
---|
156 | // I am the original function A::example().<br>
|
---|
157 | $b->example();</PRE
|
---|
158 | ></TD
|
---|
159 | ></TR
|
---|
160 | ></TABLE
|
---|
161 | ><P
|
---|
162 | ></P
|
---|
163 | ></DIV
|
---|
164 | ><P
|
---|
165 | > The above example calls the function example() in
|
---|
166 | class A, but there is no object of class A, so that
|
---|
167 | we cannot write $a->example() or similar. Instead we
|
---|
168 | call example() as a 'class function', that is, as a
|
---|
169 | function of the class itself, not any object of that
|
---|
170 | class.
|
---|
171 | </P
|
---|
172 | ><P
|
---|
173 | > There are class functions, but there are no class variables.
|
---|
174 | In fact, there is no object at all at the time of the call.
|
---|
175 | Thus, a class function may not use any object variables (but
|
---|
176 | it can use local and global variables), and it may no use
|
---|
177 | $this at all.
|
---|
178 | </P
|
---|
179 | ><P
|
---|
180 | > In the above example, class B redefines the function example().
|
---|
181 | The original definition in class A is shadowed
|
---|
182 | and no longer available, unless you are refering specifically
|
---|
183 | to the implementation of example() in class A using the
|
---|
184 | ::-operator. Write A::example() to do this (in fact, you
|
---|
185 | should be writing parent::example(), as shown in the next
|
---|
186 | section).
|
---|
187 | </P
|
---|
188 | ><P
|
---|
189 | > In this context, there is a current object and it may
|
---|
190 | have object variables. Thus, when used from WITHIN an
|
---|
191 | object function, you may use $this and object variables.
|
---|
192 | </P
|
---|
193 | ></DIV
|
---|
194 | ><DIV
|
---|
195 | CLASS="NAVFOOTER"
|
---|
196 | ><HR
|
---|
197 | ALIGN="LEFT"
|
---|
198 | WIDTH="100%"><TABLE
|
---|
199 | SUMMARY="Footer navigation table"
|
---|
200 | WIDTH="100%"
|
---|
201 | BORDER="0"
|
---|
202 | CELLPADDING="0"
|
---|
203 | CELLSPACING="0"
|
---|
204 | ><TR
|
---|
205 | ><TD
|
---|
206 | WIDTH="33%"
|
---|
207 | ALIGN="left"
|
---|
208 | VALIGN="top"
|
---|
209 | ><A
|
---|
210 | HREF="language.oop.constructor.html"
|
---|
211 | ACCESSKEY="P"
|
---|
212 | >Pøedcházející</A
|
---|
213 | ></TD
|
---|
214 | ><TD
|
---|
215 | WIDTH="34%"
|
---|
216 | ALIGN="center"
|
---|
217 | VALIGN="top"
|
---|
218 | ><A
|
---|
219 | HREF="index.html"
|
---|
220 | ACCESSKEY="H"
|
---|
221 | >Domù</A
|
---|
222 | ></TD
|
---|
223 | ><TD
|
---|
224 | WIDTH="33%"
|
---|
225 | ALIGN="right"
|
---|
226 | VALIGN="top"
|
---|
227 | ><A
|
---|
228 | HREF="keyword.parent.html"
|
---|
229 | ACCESSKEY="N"
|
---|
230 | >Dal¹í</A
|
---|
231 | ></TD
|
---|
232 | ></TR
|
---|
233 | ><TR
|
---|
234 | ><TD
|
---|
235 | WIDTH="33%"
|
---|
236 | ALIGN="left"
|
---|
237 | VALIGN="top"
|
---|
238 | ><TT
|
---|
239 | CLASS="literal"
|
---|
240 | >Constructors</TT
|
---|
241 | ></TD
|
---|
242 | ><TD
|
---|
243 | WIDTH="34%"
|
---|
244 | ALIGN="center"
|
---|
245 | VALIGN="top"
|
---|
246 | ><A
|
---|
247 | HREF="language.oop.html"
|
---|
248 | ACCESSKEY="U"
|
---|
249 | >Nahoru</A
|
---|
250 | ></TD
|
---|
251 | ><TD
|
---|
252 | WIDTH="33%"
|
---|
253 | ALIGN="right"
|
---|
254 | VALIGN="top"
|
---|
255 | ><TT
|
---|
256 | CLASS="literal"
|
---|
257 | >parent</TT
|
---|
258 | ></TD
|
---|
259 | ></TR
|
---|
260 | ></TABLE
|
---|
261 | ></DIV
|
---|
262 | ></BODY
|
---|
263 | ></HTML
|
---|
264 | >
|
---|