derick Wed Jun 18 06:55:03 2003 EDT
Modified files:
/phpdoc/en/language oop.xml
Log:
- Clarification of syntax and some structuring
Index: phpdoc/en/language/oop.xml
diff -u phpdoc/en/language/oop.xml:1.42 phpdoc/en/language/oop.xml:1.43
--- phpdoc/en/language/oop.xml:1.42 Fri Jun 13 07:24:29 2003
+++ phpdoc/en/language/oop.xml Wed Jun 18 06:55:03 2003
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
-<!-- $Revision: 1.42 $ -->
+<!-- $Revision: 1.43 $ -->
<chapter id="language.oop">
<title>Classes and Objects</title>
@@ -8,7 +8,8 @@
<para>
A class is a collection of variables and functions working with
these variables. A class is defined using the following syntax:
-
+ </para>
+ <para>
<informalexample>
<programlisting role="php">
<![CDATA[
@@ -48,17 +49,43 @@
items from this cart.
</para>
- <caution>
+ <warning>
<simpara>
- The following cautionary notes are valid for PHP 4.
+ You can <emphasis>NOT</emphasis> break up a class definition into
+ multiple files, or multiple PHP blocks. The following will not work:
</simpara>
+ <para>
+ <informalexample>
+ <programlisting>
+<![CDATA[
+<?php
+class test {
+?>
+<?php
+ function test() {
+ print 'OK';
+ }
+}
+?>
+]]>
+ </programlisting>
+ </informalexample>
+ </para>
+ </warning>
+
+ <simpara>
+ The following cautionary notes are valid for PHP 4.
+ </simpara>
+ <caution>
<simpara>
The name <literal>stdClass</literal> is used interally by
Zend and is reserved. You cannot have a class named
<literal>stdClass</literal> in PHP.
</simpara>
+ </caution>
+ <caution>
<simpara>
The function names <literal>__sleep</literal> and
<literal>__wakeup</literal> are magical in PHP classes. You
@@ -66,7 +93,9 @@
classes unless you want the magic functionality associated
with them. See below for more information.
</simpara>
+ </caution>
+ <caution>
<simpara>
PHP reserves all function names starting with __ as magical.
It is recommended that you do not use function names with
@@ -74,16 +103,15 @@
</simpara>
</caution>
- <note>
- <simpara>
- In PHP 4, only constant initializers for <literal>var</literal>
- variables are allowed. To initialize variables with non-constant
- values, you need an initialization function which is called
- automatically when an object is being constructed from the
- class. Such a function is called a constructor (see below).
- </simpara>
- <informalexample>
- <programlisting role="php">
+ <simpara>
+ In PHP 4, only constant initializers for <literal>var</literal>
+ variables are allowed. To initialize variables with non-constant
+ values, you need an initialization function which is called
+ automatically when an object is being constructed from the
+ class. Such a function is called a constructor (see below).
+ </simpara>
+ <informalexample>
+ <programlisting role="php">
<![CDATA[
<?php
/* None of these will work in PHP 4. */
@@ -112,9 +140,8 @@
}
?>
]]>
- </programlisting>
- </informalexample>
- </note>
+ </programlisting>
+ </informalexample>
<para>
Classes are types, that is, they are blueprints for actual
@@ -137,29 +164,29 @@
</informalexample>
<para>
- This creates the objects $cart and $another_cart, both of
- the class Cart. The function add_item() of the $cart object
- is being called to add 1 item of article number 10 to the
- $cart. 3 items of article number 0815 are being added to
- $another_cart.
+ This creates the objects <varname>$cart</varname> and
+ <varname>$another_cart</varname>, both of the class Cart. The function
+ add_item() of the <varname>$cart</varname> object is being called to add 1
+ item of article number 10 to the <varname>$cart</varname>. 3 items of
+ article number 0815 are being added to <varname>$another_cart</varname>.
</para>
<para>
- Both, $cart and $another_cart, have functions add_item(),
- remove_item() and a variable items. These are distinct
- functions and variables. You can think of the objects as
- something similar to directories in a filesystem. In a
- filesystem you can have two different files README.TXT, as
- long as they are in different directories. Just like with
- directories where you'll have to type the full pathname in
- order to reach each file from the toplevel directory, you
- have to specify the complete name of the function you want
- to call: In PHP terms, the toplevel directory would be the
- global namespace, and the pathname separator would be ->.
- Thus, the names $cart->items and $another_cart->items
- name two different variables. Note that the variable is
- named $cart->items, not $cart->$items, that is, a
- variable name in PHP has only a single dollar sign.
+ Both, <varname>$cart</varname> and <varname>$another_cart</varname>, have
+ functions add_item(), remove_item() and a variable items. These are
+ distinct functions and variables. You can think of the objects as
+ something similar to directories in a filesystem. In a filesystem you can
+ have two different files README.TXT, as long as they are in different
+ directories. Just like with directories where you'll have to type the
+ full pathname in order to reach each file from the toplevel directory, you
+ have to specify the complete name of the function you want to call: In PHP
+ terms, the toplevel directory would be the global namespace, and the
+ pathname separator would be <literal>-></literal>. Thus, the names
+ <varname>$cart->items</varname> and
+ <varname>$another_cart->items</varname> name two different variables.
+ Note that the variable is named <varname>$cart->items</varname>, not
+ <varname>$cart->$items</varname>, that is, a variable name in PHP has
+ only a single dollar sign.
</para>
<informalexample>
@@ -182,17 +209,19 @@
</informalexample>
<para>
- Within a class definition, you do not know under which name the object will
- be accessible in your program: at the time the Cart class was
- written, it was unknown that the object will be named $cart or
- $another_cart later. Thus, you cannot write $cart->items within
- the Cart class itself. Instead, in order to be able to access it's own
- functions and variables from within a class, one can use the
- pseudo-variable $this which can be read as 'my own' or
- 'current object'. Thus, '$this->items[$artnr] += $num' can
- be read as 'add $num to the $artnr counter of my own items
- array' or 'add $num to the $artnr counter of the items array
- within the current object'.
+ Within a class definition, you do not know under which name the object
+ will be accessible in your program: at the time the Cart class was
+ written, it was unknown that the object will be named
+ <varname>$cart</varname> or <varname>$another_cart</varname> later. Thus,
+ you cannot write <varname>$cart->items</varname> within the Cart class
+ itself. Instead, in order to be able to access it's own functions and
+ variables from within a class, one can use the pseudo-variable
+ <varname>$this</varname> which can be read as 'my own' or 'current
+ object'. Thus, '<varname>$this->items[$artnr]</varname> +=
+ <varname>$num</varname>' can be read as 'add <varname>$num</varname> to
+ the <varname>$artnr</varname> counter of my own items array' or 'add
+ <varname>$num</varname> to the <varname>$artnr</varname> counter of the
+ items array within the current object'.
</para>
<note>
@@ -242,11 +271,11 @@
</informalexample>
<para>
- This defines a class Named_Cart that has all variables and
- functions of Cart plus an additional variable $owner and an
- additional function set_owner(). You create a named cart the usual
- way and can now set and get the carts owner. You can still use
- normal cart functions on named carts:
+ This defines a class Named_Cart that has all variables and functions of
+ Cart plus an additional variable <varname>$owner</varname> and an
+ additional function set_owner(). You create a named cart the usual way and
+ can now set and get the carts owner. You can still use normal cart
+ functions on named carts:
</para>
<informalexample>
@@ -552,7 +581,7 @@
In fact, there is no object at all at the time of the call.
Thus, a class function may not use any object variables (but
it can use local and global variables), and it may no use
- $this at all.
+ <varname>$this</varname> at all.
</para>
<para>
@@ -566,9 +595,9 @@
</para>
<para>
- In this context, there is a current object and it may
- have object variables. Thus, when used from WITHIN an
- object function, you may use $this and object variables.
+ In this context, there is a current object and it may have object
+ variables. Thus, when used from WITHIN an object function, you may use
+ <varname>$this</varname> and object variables.
</para>
</sect1>
@@ -656,16 +685,15 @@
</para>
<para>
- In order to be able to <function>unserialize</function> an
- object, the class of that object needs to be defined. That
- is, if you have an object $a of class A on page1.php and
- serialize this, you'll get a string that refers to class A
- and contains all values of variabled contained in $a. If
- you want to be able to unserialize this on page2.php,
- recreating $a of class A, the definition of class A must
- be present in page2.php. This can be done for example
- by storing the class defintion of class A in an include
- file and including this file in both page1.php and page2.php.
+ In order to be able to <function>unserialize</function> an object, the
+ class of that object needs to be defined. That is, if you have an object
+ <varname>$a</varname> of class A on page1.php and serialize this, you'll
+ get a string that refers to class A and contains all values of variabled
+ contained in <varname>$a</varname>. If you want to be able to unserialize
+ this on page2.php, recreating <varname>$a</varname> of class A, the
+ definition of class A must be present in page2.php. This can be done for
+ example by storing the class defintion of class A in an include file and
+ including this file in both page1.php and page2.php.
</para>
<informalexample>
@@ -731,10 +759,10 @@
</para>
<para>
- So if in the example above $a became part of a session by
- running <literal>session_register("a")</literal>, you should
- include the file <literal>classa.inc</literal> on all of your
- pages, not only page1.php and page2.php.
+ So if in the example above <varname>$a</varname> became part of a session
+ by running <literal>session_register("a")</literal>, you should include the
+ file <literal>classa.inc</literal> on all of your pages, not only page1.php
+ and page2.php.
</para>
</sect1>
--
PHP Documentation Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php