source: www/manuals/PHP_manual/language.oop.newref.html@ 1

Last change on this file since 1 was 1, checked in by george, 17 years ago

Prvotní import všeho

File size: 6.8 KB
Line 
1<HTML
2><HEAD
3><TITLE
4>References inside the constructor</TITLE
5><META
6NAME="GENERATOR"
7CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
8REL="HOME"
9TITLE="Manuál PHP"
10HREF="index.html"><LINK
11REL="UP"
12TITLE="Classes and Objects"
13HREF="language.oop.html"><LINK
14REL="PREVIOUS"
15TITLE="The magic functions __sleep and __wakeup"
16HREF="language.oop.magic-functions.html"><LINK
17REL="NEXT"
18TITLE="Vysvìtlení referencí (odkazù)"
19HREF="language.references.html"><META
20HTTP-EQUIV="Content-type"
21CONTENT="text/html; charset=ISO-8859-2"></HEAD
22><BODY
23CLASS="sect1"
24BGCOLOR="#FFFFFF"
25TEXT="#000000"
26LINK="#0000FF"
27VLINK="#840084"
28ALINK="#0000FF"
29><DIV
30CLASS="NAVHEADER"
31><TABLE
32SUMMARY="Header navigation table"
33WIDTH="100%"
34BORDER="0"
35CELLPADDING="0"
36CELLSPACING="0"
37><TR
38><TH
39COLSPAN="3"
40ALIGN="center"
41>Manuál PHP</TH
42></TR
43><TR
44><TD
45WIDTH="10%"
46ALIGN="left"
47VALIGN="bottom"
48><A
49HREF="language.oop.magic-functions.html"
50ACCESSKEY="P"
51>Pøedcházející</A
52></TD
53><TD
54WIDTH="80%"
55ALIGN="center"
56VALIGN="bottom"
57>Kapitola 14. Classes and Objects</TD
58><TD
59WIDTH="10%"
60ALIGN="right"
61VALIGN="bottom"
62><A
63HREF="language.references.html"
64ACCESSKEY="N"
65>Dal¹í</A
66></TD
67></TR
68></TABLE
69><HR
70ALIGN="LEFT"
71WIDTH="100%"></DIV
72><DIV
73CLASS="sect1"
74><H1
75CLASS="sect1"
76><A
77NAME="language.oop.newref"
78></A
79>References inside the constructor</H1
80><P
81>&#13; Creating references within the constructor can lead to confusing
82 results. This tutorial-like section helps you to avoid problems.
83
84 <DIV
85CLASS="informalexample"
86><A
87NAME="AEN5790"
88></A
89><P
90></P
91><TABLE
92BORDER="0"
93BGCOLOR="#E0E0E0"
94CELLPADDING="5"
95><TR
96><TD
97><PRE
98CLASS="php"
99>class Foo
100{
101 function Foo($name)
102 {
103 // create a reference inside the global array $globalref
104 global $globalref;
105 $globalref[] = &#38;$this;
106 // set name to passed value
107 $this-&#62;setName($name);
108 // and put it out
109 $this-&#62;echoName();
110 }
111
112 function echoName()
113 {
114 echo "&#60;br&#62;",$this-&#62;name;
115 }
116
117 function setName($name)
118 {
119 $this-&#62;name = $name;
120 }
121}</PRE
122></TD
123></TR
124></TABLE
125><P
126></P
127></DIV
128>
129 </P
130><P
131>&#13; Let us check out if there is a difference between
132 <TT
133CLASS="varname"
134>$bar1</TT
135> which has been created using
136 the copy <TT
137CLASS="literal"
138>=</TT
139> operator and
140 <TT
141CLASS="varname"
142>$bar2</TT
143> which has been created using
144 the reference <TT
145CLASS="literal"
146>=&#38;</TT
147> operator...
148
149 <DIV
150CLASS="informalexample"
151><A
152NAME="AEN5797"
153></A
154><P
155></P
156><TABLE
157BORDER="0"
158BGCOLOR="#E0E0E0"
159CELLPADDING="5"
160><TR
161><TD
162><PRE
163CLASS="php"
164>$bar1 = new Foo('set in constructor');
165$bar1-&#62;echoName();
166$globalref[0]-&#62;echoName();
167
168/* output:
169set in constructor
170set in constructor
171set in constructor */
172
173$bar2 =&#38; new Foo('set in constructor');
174$bar2-&#62;echoName();
175$globalref[1]-&#62;echoName();
176
177/* output:
178set in constructor
179set in constructor
180set in constructor */</PRE
181></TD
182></TR
183></TABLE
184><P
185></P
186></DIV
187>
188 </P
189><P
190>&#13; Apparently there is no difference, but in fact there is a
191 very significant one: <TT
192CLASS="varname"
193>$bar1</TT
194> and
195 <TT
196CLASS="varname"
197>$globalref[0]</TT
198> are _NOT_ referenced, they
199 are NOT the same variable. This is because "new" does not
200 return a reference by default, instead it returns a copy.
201 <DIV
202CLASS="note"
203><BLOCKQUOTE
204CLASS="note"
205><P
206><B
207>Poznámka: </B
208>
209 There is no performance loss (since PHP 4 and up use reference
210 counting) returning copies instead of references. On the
211 contrary it is most often better to simply work with copies
212 instead of references, because creating references takes some
213 time where creating copies virtually takes no time (unless none
214 of them is a large array or object and one of them gets changed
215 and the other(s) one(s) subsequently, then it would be wise to
216 use references to change them all concurrently).
217 </P
218></BLOCKQUOTE
219></DIV
220>
221 To prove what is written above let us watch the code below.
222
223 <DIV
224CLASS="informalexample"
225><A
226NAME="AEN5804"
227></A
228><P
229></P
230><TABLE
231BORDER="0"
232BGCOLOR="#E0E0E0"
233CELLPADDING="5"
234><TR
235><TD
236><PRE
237CLASS="php"
238>// now we will change the name. what do you expect?
239// you could expect that both $bar1 and $globalref[0] change their names...
240$bar1-&#62;setName('set from outside');
241
242// as mentioned before this is not the case.
243$bar1-&#62;echoName();
244$globalref[0]-&#62;echoName();
245
246/* output:
247set from outside
248set in constructor */
249
250// let us see what is different with $bar2 and $globalref[1]
251$bar2-&#62;setName('set from outside');
252
253// luckily they are not only equal, they are the same variable
254// thus $bar2-&#62;name and $globalref[1]-&#62;name are the same too
255$bar2-&#62;echoName();
256$globalref[1]-&#62;echoName();
257
258/* output:
259set from outside
260set from outside */</PRE
261></TD
262></TR
263></TABLE
264><P
265></P
266></DIV
267>
268 </P
269><P
270>&#13; Another final example, try to understand it.
271
272 <DIV
273CLASS="informalexample"
274><A
275NAME="AEN5807"
276></A
277><P
278></P
279><TABLE
280BORDER="0"
281BGCOLOR="#E0E0E0"
282CELLPADDING="5"
283><TR
284><TD
285><PRE
286CLASS="php"
287>class A
288{
289 function A($i)
290 {
291 $this-&#62;value = $i;
292 // try to figure out why we do not need a reference here
293 $this-&#62;b = new B($this);
294 }
295
296 function createRef()
297 {
298 $this-&#62;c = new B($this);
299 }
300
301 function echoValue()
302 {
303 echo "&#60;br&#62;","class ",get_class($this),': ',$this-&#62;value;
304 }
305}
306
307
308class B
309{
310 function B(&#38;$a)
311 {
312 $this-&#62;a = &#38;$a;
313 }
314
315 function echoValue()
316 {
317 echo "&#60;br&#62;","class ",get_class($this),': ',$this-&#62;a-&#62;value;
318 }
319}
320
321// try to undestand why using a simple copy here would yield
322// in an undesired result in the *-marked line
323$a =&#38; new A(10);
324$a-&#62;createRef();
325
326$a-&#62;echoValue();
327$a-&#62;b-&#62;echoValue();
328$a-&#62;c-&#62;echoValue();
329
330$a-&#62;value = 11;
331
332$a-&#62;echoValue();
333$a-&#62;b-&#62;echoValue(); // *
334$a-&#62;c-&#62;echoValue();
335
336/*
337output:
338class A: 10
339class B: 10
340class B: 10
341class A: 11
342class B: 11
343class B: 11
344*/</PRE
345></TD
346></TR
347></TABLE
348><P
349></P
350></DIV
351>
352 </P
353></DIV
354><DIV
355CLASS="NAVFOOTER"
356><HR
357ALIGN="LEFT"
358WIDTH="100%"><TABLE
359SUMMARY="Footer navigation table"
360WIDTH="100%"
361BORDER="0"
362CELLPADDING="0"
363CELLSPACING="0"
364><TR
365><TD
366WIDTH="33%"
367ALIGN="left"
368VALIGN="top"
369><A
370HREF="language.oop.magic-functions.html"
371ACCESSKEY="P"
372>Pøedcházející</A
373></TD
374><TD
375WIDTH="34%"
376ALIGN="center"
377VALIGN="top"
378><A
379HREF="index.html"
380ACCESSKEY="H"
381>Domù</A
382></TD
383><TD
384WIDTH="33%"
385ALIGN="right"
386VALIGN="top"
387><A
388HREF="language.references.html"
389ACCESSKEY="N"
390>Dal¹í</A
391></TD
392></TR
393><TR
394><TD
395WIDTH="33%"
396ALIGN="left"
397VALIGN="top"
398>The magic functions <TT
399CLASS="literal"
400>__sleep</TT
401> and <TT
402CLASS="literal"
403>__wakeup</TT
404></TD
405><TD
406WIDTH="34%"
407ALIGN="center"
408VALIGN="top"
409><A
410HREF="language.oop.html"
411ACCESSKEY="U"
412>Nahoru</A
413></TD
414><TD
415WIDTH="33%"
416ALIGN="right"
417VALIGN="top"
418>Vysvìtlení referencí (odkazù)</TD
419></TR
420></TABLE
421></DIV
422></BODY
423></HTML
424>
Note: See TracBrowser for help on using the repository browser.