| 12
 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
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 
 | <?xml version="1.0" encoding="big5"?>
 <chapter id="language.oop">
  <title>OM</title>
  <sect1 id="keyword.class">
   <title><literal>O</literal></title>
   <para>
    O²ӻNO@pq(]iO@jq)@ӦW٪{qA 䤺ܼƥHΨϥγoܼƪơC wqOykЬݤUCܽdG 
    <informalexample>
     <programlisting role="php">
<?php
class Cart {
    var $items;  // Items in our shopping cart
   
    // Add $num articles of $artnr to the cart
 
    function add_item ($artnr, $num) {
        $this->items[$artnr] += $num;
    }
   
    // Take $num articles of $artnr out of the cart
 
    function remove_item ($artnr, $num) {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } else {
            return false;
        }   
    }
}
?>
     </programlisting>
    </informalexample>
   </para>
 
   <para>
    WwqF@ӥs Cart O(: Wʪ)C ]AF@ӦWٯަ}CHӤOΨӦb}C[JΦ~ơC 
   </para>
   <note>
    <simpara>
     In PHP 4, only constant initializers for <literal>var</literal>
     variables are allowed. Use constructors for non-constant
     initializers.  
    </simpara>
    <informalexample>
     <programlisting role="php">
/* None of these will work in PHP 4. */
class Cart {
    var $todays_date = date("Y-m-d");
    var $name = $firstname;
    var $owner = 'Fred ' . 'Jones';
}
/* This is how it should be done. */
class Cart {
    var $todays_date;
    var $name;
    var $owner;
    function Cart() {
        $this->todays_date = date("Y-m-d");
        $this->name = $GLOBALS['firstname'];
        /* etc. . . */
    }
}
     </programlisting>
    </informalexample>
   </note>
   <para>
    OO@ܼƪκAC ]NO integerB string HYOκAܼơC POإ߬YOܼƻݭnΨ new O (: o˲ܼͪƳNyWsإߤF@Ӫ)G 
   </para>
 
   <informalexample>
    <programlisting role="php">
$cart = new Cart;
$cart->add_item("10", 1);
    </programlisting>
   </informalexample>
 
   <para>
    WҫإߤF@ӥs cart A OO $CartC (ٰOo PHP jpgO@˪ ?) ۽եθӪ add_item Ʀb}C[J@ӽs '10' FC 
</para><para> O]iHOLOC sO~ӤFªO~٥iH[WۤvܼƤΥ\C wqOkOwqOɥ[W extend orC Lǩwh~YC 
   </para>
 
   <informalexample>
    <programlisting role="php">
class Named_Cart extends Cart {
    var $owner;
  
    function set_owner ($name) {
        $this->owner = $name;
    }
}
    </programlisting>
   </informalexample>
 
   <para>
    WswqF Named_Cart OA ֦ Cart OҦܼƤΨƥt[W@ӦsΤWrܼƤΦsܼƪơG set_owner()C AiΦѤkھڷsOإߤ@ӪA {boӪFiHHe@˥[F~٥iH]wΤWrG 
   </para>
 
   <informalexample>
    <programlisting role="php">
$ncart = new Named_Cart;    // Create a named cart
$ncart->set_owner ("kris"); // Name that cart
print $ncart->owner;        // print the cart owners name
$ncart->add_item ("10", 1); // (inherited functionality from cart)
    </programlisting>
   </informalexample>
  
   <para>
   bwqO $this oܼƬOC bA@wn $this->something yӽեΩΦsܼƩMơC 
C
Both in and
    outside of the object you do not need a $ when accessing an object's
    properties.
   </para>
   <informalexample>
    <programlisting role="php">
$ncart->owner  = "chris"; // no $
$ncart->$owner = "chris";
// this is invalid because $ncart->$owner = $ncart->""
$myvar = 'owner';
$ncart->$myvar = "chris";  
// this is valid because $ncart->$myvar = $ncart->owner
    </programlisting>
   </informalexample>
   <para>
    Constructors سy OO@ӨơC |bCӮھڸOͪɦ۰ʳQC ܦسy̨ƪkOӨƩRWMOWr@ˡC 
   </para>
 
   <informalexample>
    <programlisting role="php">
class Auto_Cart extends Cart {
    function Auto_Cart () {
        $this->add_item ("10", 1);
    }
}
    </programlisting>
   </informalexample>
 
   <para>
    Wڭ̩wqF Auto_Cart OC O@ Cart O[WF@ӫسy̨ơA oƷ|۰ʷ|b}C (Ϊ̻Oʪ) [W@ '10' FC t~سy̨Ƥ]iHѼơA oOإߪɦjuʡC 
   </para>
 
   <informalexample>
    <programlisting role="php">
class Constructor_Cart extends Cart {
    function Constructor_Cart ($item = "10", $num = 1) {
        $this->add_item ($item, $num);
    }
}
 
// Shop the same old boring stuff.
 
$default_cart   = new Constructor_Cart;
 
// Shop for real...
 
$different_cart = new Constructor_Cart ("20", 17);
    </programlisting>
   </informalexample>
 
   <caution>
    <simpara>
     ~OӨA pGlOMOسy̨ƸܡA إ~Oɨä|PɽեΥOسy̨ơC 
    </simpara> 
   </caution>
  </sect1>
  
 <sect1 id="language.oop.newref">
   <title>References inside the constructor</title>
   <para>
    Creating references within the constructor can lead to confusing
    results. This tutorial-like section helps you to avoid problems.
 
    <informalexample>
     <programlisting role="php">
class foo {
    function foo($name) {
        // create a reference inside the global array $globalref
        global $globalref;
		$globalref[] = &$this;
        // set name to passed value
        $this->setName($name);
		// and put it out
        $this->echoName();
    }
    function echoName() {
        echo "<br>",$this->Name;
    }
	
	function setName($name)	{
		$this->Name = $name;
    }
}
    </programlisting>
   </informalexample>
  </para>
    
   <para>
   Let us check out if there is a difference between <varname>$bar1</varname> which has been created using the copy <literal>=</literal> operator
   and <varname>$bar2</varname> which has been created using the reference <literal>=&</literal> operator...
   
    <informalexample>
     <programlisting role="php">
   
    $bar1 = new foo('set in constructor');
    $bar1->echoName();
    $globalref[0]->echoName();
    
    /* output:
    set in constructor
    set in constructor
    set in constructor */
    
    $bar2 =& new foo('set in constructor');
    $bar2->echoName();
    $globalref[1]->echoName();
    /* output:
    set in constructor
    set in constructor
    set in constructor */
    
        </programlisting>
   </informalexample>
  </para>
   <para>
    Apparently there is no difference, but in fact there is a very significant one:
    <varname>$bar1</varname> and <varname>$globalref[0]</varname> are _NOT_ referenced, they are NOT the same variable. 
    This is because "new" does not return a reference by default, instead it returns a copy.
       <note>
         <simpara>
         There is no performance loss (since php 4 and up use reference counting) returning copies instead of references.
         On the contrary it is most often better to simply work with copies instead of references, because creating
         references takes some time where creating copies virtually takes no time (unless none of them is a large array or object
         and one of them gets changed and the other(s) one(s) subsequently, then it would be wise to use references to change them
         all concurrently).
         </simpara>
        </note>
    To prove what is written above let us watch the code below.
   
   <informalexample>
     <programlisting role="php">
        // now we will change the name. what do you expect?
        // you could expect that both $bar and $globalref[0] change their names...
        $bar1->setName('set from outside');
        // as mentioned before this is not the case.
        $bar1->echoName();
        $globalref[0]->echoName();
        /* output:
        set on object creation
        set from outside */
        // let us see what is different with $bar2 and $globalref[1]
        $bar2->setName('set from outside');
        // luckily they are not only equyl, they are thesame variable
        // thus $bar2->Name and $globalref[1]->Name are the same too
        $bar2->echoName();
        $globalref[1]->echoName();
        /* output:
        set from outside
        set from outside */
        
      </programlisting>
   </informalexample>   
 </para>   
   <para>
   Another final example, try to understand it.
   
   <informalexample>
     <programlisting role="php">
class a {
    function a($i) {
        $this->value = $i;
        // try to figure out why we do not need a reference here
        $this->b = new b($this);
    }
    function createRef() {
        $this->c = new b($this);
    }
    function echoValue() {
        echo "<br>","class ",get_class($this),': ',$this->value;
        }
}
class b  {
    function b(&$a) {
        $this->a = &$a;
    }
   function echoValue() {
        echo "<br>","class ",get_class($this),': ',$this->a->value;
        }
}
// try to undestand why using a simple copy here would yield
// in an undesired result in the *-marked line
$a =& new a(10);
$a->createRef();
$a->echoValue();
$a->b->echoValue();
$a->c->echoValue();
$a->value = 11;
$a->echoValue();
$a->b->echoValue(); // *
$a->c->echoValue();
/*
output:
class a: 10
class b: 10
class b: 10
class a: 11
class b: 11
class b: 11
*/
      </programlisting>
   </informalexample>
  </para>
</sect1>
 
 </chapter>
 
 <!-- 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:
 -->
 |