CVS update: php3/doc/chapters
| From: | eschmid | Date: | Thu, 24 Dec 1998 15:04:38 +0000 |
| Subject: | CVS update: php3/doc/chapters | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-3031@lists.php.net to get a copy of this message | ||
Date: Thursday December 24, 1998 @ 10:04
Author: eschmid
Update of /repository/php3/doc/chapters
In directory asf:/u/temp/cvs-serv24258
Modified Files:
lang-const.sgml
Log Message:
Identation and a little typo
Index: php3/doc/chapters/lang-const.sgml
diff -c php3/doc/chapters/lang-const.sgml:1.29 php3/doc/chapters/lang-const.sgml:1.30
*** php3/doc/chapters/lang-const.sgml:1.29 Wed Dec 9 21:07:28 1998
--- php3/doc/chapters/lang-const.sgml Thu Dec 24 10:04:38 1998
***************
*** 162,195 ****
the value of $a), '$b *= 7' (multiply the value of $b by 7), etc.
<para>
! There is one more expression that may seem odd if you haven't
! seen it in other languages, the terniary conditional operator:
! <informalexample><programlisting>
$first ? $second : $third
</programlisting></informalexample>
! If the value of the first subexpression is true (non-zero), then
! it the second subexpression is evaluated, and that is the result of
! the conditional expression. Otherwise, the third subexpression is
! evaluated, and that is the value.
<para>
The following example should help you understand pre- and
post-increment and expressions in general a bit better:
<informalexample>
<programlisting>
! function double($i)
! {
! return $i*2;
}
$b = $a = 5; /* assign the value five into the variable $a and $b */
! $c = $a++; /* post-increment, assign original value of $a (5) to $c */
! $e = $d = ++$b; /* pre-increment, assign the incremented value of $b (6) to $d and $e */
/* at this point, both $d and $e are equal to 6 */
! $f = double($d++); /* assign twice the value of $d <emphasis>before</emphasis> the
increment, 2*6 = 12 to $f */
! $g = double(++$e); /* assign twice the value of $e <emphasis>after</emphasis> the
increment, 2*7 = 14 to $f */
! $h = $g += 10; /* first, $g is incremented by 10 and ends with the value of 24.
! the value of the assignment (24) is then assigned into $h,
! and $h ends with the value of 24 as well. */
</programlisting></informalexample>
<simpara>
--- 162,203 ----
the value of $a), '$b *= 7' (multiply the value of $b by 7), etc.
<para>
! There is one more expression that may seem odd if you haven't seen
! it in other languages, the terniary conditional operator:
!
! <informalexample><programlisting>
$first ? $second : $third
</programlisting></informalexample>
!
! If the value of the first subexpression is true (non-zero), then
! it the second subexpression is evaluated, and that is the result
! of the conditional expression. Otherwise, the third subexpression
! is evaluated, and that is the value.
<para>
The following example should help you understand pre- and
post-increment and expressions in general a bit better:
<informalexample>
<programlisting>
! function double($i) {
! return $i*2;
}
$b = $a = 5; /* assign the value five into the variable $a and $b */
! $c = $a++; /* post-increment, assign original value of $a
! (5) to $c */
! $e = $d = ++$b; /* pre-increment, assign the incremented value of
! $b (6) to $d and $e */
!
/* at this point, both $d and $e are equal to 6 */
!
! $f = double($d++); /* assign twice the value of $d <emphasis>before</emphasis>
! the increment, 2*6 = 12 to $f */
! $g = double(++$e); /* assign twice the value of $e <emphasis>after</emphasis>
! the increment, 2*7 = 14 to $g */
! $h = $g += 10; /* first, $g is incremented by 10 and ends with the
! value of 24. the value of the assignment (24) is
! then assigned into $h, and $h ends with the value
! of 24 as well. */
</programlisting></informalexample>
<simpara>
***************
*** 241,246 ****
--- 249,255 ----
<para>
The following example would display 'a is bigger than b' if $a is
bigger than $b:
+
<informalexample><programlisting>
if ($a > $b)
print "a is bigger than b";
***************
*** 253,258 ****
--- 262,268 ----
a statement group. For example, this code would display 'a is
bigger than b' if $a is bigger than $b, and would then assign the
value of $a into $b:
+
<informalexample><programlisting>
if ($a>$b) {
print "a is bigger than b";
***************
*** 275,288 ****
statement in case the expression in the IF statement evaluates to
FALSE. For example, the following code would display 'a is bigger
than b' if $a is bigger than $b, and 'a is NOT bigger than b'
! otherwise:
! <informalexample><programlisting>
! if ($a>$b) {
! print "a is bigger than b";
! } else {
! print "a is NOT bigger than b";
}
! </programlisting></informalexample>
The ELSE statement is only executed if the IF expression evaluated
to FALSE, and if there were any ELSEIF expressions - only if they
--- 285,301 ----
statement in case the expression in the IF statement evaluates to
FALSE. For example, the following code would display 'a is bigger
than b' if $a is bigger than $b, and 'a is NOT bigger than b'
! otherwise:
!
! <informalexample>
! <programlisting>
! if ($a > $b) {
! print "a is bigger than b";
! } else {
! print "a is NOT bigger than b";
}
!
! </programlisting></informalexample>
The ELSE statement is only executed if the IF expression evaluated
to FALSE, and if there were any ELSEIF expressions - only if they
***************
*** 291,304 ****
<sect1 id="construct.elseif">
<title>ELSEIF</title>
! <para>ELSEIF, as its name suggests, is a combination of IF and
! ELSE. Like ELSE, it extends an IF statement to execute a
! different statement in case the original IF expression evaluates
! to FALSE. However, unlike ELSE, it will execute that alternative
! expression only if the ELSEIF expression evaluates to TRUE. For
! example, the following code would display 'a is bigger than b' if
! $a>$b, 'a is equal to b' if $a==$b, and 'a is smaller than b' if
! $a<$b:
<informalexample><programlisting>
if ($a > $b) {
print "a is bigger than b";
--- 304,318 ----
<sect1 id="construct.elseif">
<title>ELSEIF</title>
! <para>
! ELSEIF, as its name suggests, is a combination of IF and ELSE.
! Like ELSE, it extends an IF statement to execute a different
! statement in case the original IF expression evaluates to FALSE.
! However, unlike ELSE, it will execute that alternative expression
! only if the ELSEIF expression evaluates to TRUE. For example, the
! following code would display 'a is bigger than b' if $a>$b, 'a is
! equal to b' if $a==$b, and 'a is smaller than b' if $a<$b:
!
<informalexample><programlisting>
if ($a > $b) {
print "a is bigger than b";
***************
*** 326,350 ****
<sect2>
<title>Alternative syntax for IF statements: IF(): ... ENDIF;</title>
! <para>PHP 3 offers a different way to group statements within an IF
statement. This is most commonly used when you nest HTML blocks
inside IF statements, but can be used anywhere. Instead of using
! curly braces, the IF(expr) should be followed by a colon, the list
! of one or more statements, and end with ENDIF;. Consider the
! following example:
<informalexample><programlisting>
<?php if ($a==5): ?>
A = 5
<?php endif; ?>
</programlisting></informalexample>
! <simpara>In the above example, the HTML block "A = 5" is nested
! within an IF statement written in the alternative syntax. The
! HTML block would be displayed only if $a is equal to 5.
!
! <para>The alternative syntax applies to ELSE and ELSEIF (expr) as
! well. The following is an IF statement with ELSEIF and ELSE in
! the alternative format:
<informalexample><programlisting>
if ($a==5):
print "a equals 5";
--- 340,369 ----
<sect2>
<title>Alternative syntax for IF statements: IF(): ... ENDIF;</title>
! <para>
! PHP 3 offers a different way to group statements within an IF
statement. This is most commonly used when you nest HTML blocks
inside IF statements, but can be used anywhere. Instead of using
! curly braces, the IF(expr) should be followed by a colon, the
! list of one or more statements, and end with ENDIF;. Consider
! the following example:
!
<informalexample><programlisting>
<?php if ($a==5): ?>
A = 5
<?php endif; ?>
</programlisting></informalexample>
+
+ <simpara>
+ In the above example, the HTML block "A = 5" is nested within an
+ IF statement written in the alternative syntax. The HTML block
+ would be displayed only if $a is equal to 5.
! <para>
! The alternative syntax applies to ELSE and ELSEIF (expr) as well.
! The following is an IF statement with ELSEIF and ELSE in the
! alternative format:
!
<informalexample><programlisting>
if ($a==5):
print "a equals 5";
***************
*** 362,369 ****
<para>
WHILE loops are the simplest type of loop in PHP 3. They behave
! just like their C counterparts. The basic form of a WHILE
! statement is:
<informalexample><programlisting>
WHILE(expr) statement
</programlisting></informalexample>
--- 381,389 ----
<para>
WHILE loops are the simplest type of loop in PHP 3. They behave
! just like their C counterparts. The basic form of a WHILE
! statement is:
!
<informalexample><programlisting>
WHILE(expr) statement
</programlisting></informalexample>
***************
*** 379,395 ****
if the WHILE expression evaluates to FALSE from the very
beginning, the nested statement(s) won't even be run once.
! <para>
! Like with the IF statement, you can group multiple statements
! within the same WHILE loop by surrounding a group of statements
! with curly braces, OR by using the alternate syntax:
<informalexample><programlisting>
WHILE(expr): statement ... ENDWHILE;
</programlisting></informalexample>
<para>
! The following examples are identical, and both print numbers from
! 1 to 10:
<informalexample><programlisting>
/* example 1 */
$i=1;
--- 399,417 ----
if the WHILE expression evaluates to FALSE from the very
beginning, the nested statement(s) won't even be run once.
! <para>
! Like with the IF statement, you can group multiple statements
! within the same WHILE loop by surrounding a group of statements
! with curly braces, OR by using the alternate syntax:
!
<informalexample><programlisting>
WHILE(expr): statement ... ENDWHILE;
</programlisting></informalexample>
<para>
! The following examples are identical, and both print numbers from
! 1 to 10:
!
<informalexample><programlisting>
/* example 1 */
$i=1;
***************
*** 422,427 ****
--- 444,450 ----
the loop execution would end immediately).
<para>
There is just one syntax for DO..WHILE loops:
+
<informalexample><programlisting>
$i = 0;
do {
***************
*** 465,470 ****
--- 488,494 ----
<para>
FOR loops are the most complex loops in PHP. They behave like
their C counterparts. The syntax of a FOR loop is:
+
<informalexample><programlisting>
FOR (expr1; expr2; expr3) statement
</programlisting></informalexample>
***************
*** 488,493 ****
--- 512,518 ----
<para>
Consider the following examples. All of them display numbers from
1 to 10:
+
<informalexample><programlisting>
/* example 1 */
for ($i=1; $i<=10; $i++) {
***************
*** 518,544 ****
</programlisting></informalexample>
<simpara>
! Of course, the first example appears to be the nicest one (or perhaps
! the fourth), but you may find that being able to use empty expressions
! in FOR loops comes in handy in many occasions.
<para>
PHP also supports the alternate "colon syntax" for FOR loops.
<informalexample><programlisting>
FOR (expr1; expr2; expr3): statement; ...; endfor;
</programlisting></informalexample>
<para>
! Other languages have a foreach statement to traverse an array
! or hash. PHP uses the while statement and the <function>list</function>
! and <function>each</function> functions for this. See the documentation
! for these functions for an example.
<sect1 id="construct.break">
<title>BREAK</title>
<para>
BREAK breaks out of the current looping construct.
<informalexample><programlisting>
$i = 0;
while ($i < 10) {
--- 543,572 ----
</programlisting></informalexample>
<simpara>
! Of course, the first example appears to be the nicest one (or
! perhaps the fourth), but you may find that being able to use empty
! expressions in FOR loops comes in handy in many occasions.
<para>
PHP also supports the alternate "colon syntax" for FOR loops.
+
<informalexample><programlisting>
FOR (expr1; expr2; expr3): statement; ...; endfor;
</programlisting></informalexample>
<para>
! Other languages have a foreach statement to traverse an array or
! hash. PHP uses the while statement and the
! <function>list</function> and <function>each</function> functions
! for this. See the documentation for these functions for an
! example.
<sect1 id="construct.break">
<title>BREAK</title>
<para>
BREAK breaks out of the current looping construct.
+
<informalexample><programlisting>
$i = 0;
while ($i < 10) {
***************
*** 554,559 ****
--- 582,588 ----
<para>
CONTINUE jumps to the beginning of enclosing the looping construct.
+
<informalexample><programlisting>
while (list($key,$value) = each($arr)) {
if ($key % 2) { // skip even members
***************
*** 576,581 ****
--- 605,611 ----
The following two examples are two different ways to write the
same thing, one using a series of IF statements, and the other
using the SWITCH statement:
+
<informalexample><programlisting>
/* example 1 */
if ($i == 0) {
***************
*** 602,608 ****
}
</programlisting></informalexample>
! <para>
It is important to understand how the SWITCH statement is executed
in order to avoid messups. The SWITCH statement executes line by
line (actually, statement by statement). In the beginning, no
--- 632,638 ----
}
</programlisting></informalexample>
! <para>
It is important to understand how the SWITCH statement is executed
in order to avoid messups. The SWITCH statement executes line by
line (actually, statement by statement). In the beginning, no
***************
*** 613,627 ****
sees a BREAK statement. If you don't write a BREAK statement at
the end of a case's statement list, PHP will go on executing the
statements of the following case. For example:
<informalexample><programlisting>
/* example 3 */
switch ($i) {
! case 0:
! print "i equals 0";
! case 1:
! print "i equals 1";
! case 2:
! print "i equals 2";
}
</programlisting></informalexample>
--- 643,658 ----
sees a BREAK statement. If you don't write a BREAK statement at
the end of a case's statement list, PHP will go on executing the
statements of the following case. For example:
+
<informalexample><programlisting>
/* example 3 */
switch ($i) {
! case 0:
! print "i equals 0";
! case 1:
! print "i equals 1";
! case 2:
! print "i equals 2";
}
</programlisting></informalexample>
***************
*** 637,642 ****
--- 668,674 ----
<para>
A special case is the default case. This case matches anything
that wasn't matched by the other cases. For example:
+
<informalexample><programlisting>
/* example 4 */
switch ($i) {
***************
*** 683,705 ****
This happens each time the INCLUDE statement is encountered,
so you can use an INCLUDE statement within a looping structure
to include a number of different file.
<informalexample><programlisting>
$files = array('first.inc', 'second.inc', 'third.inc');
for ($i = 0; $i < count($files); $i++) {
include($files[$i]);
}
</programlisting></informalexample>
<para>
! <function>include</function> differs from <function>require</function>
in that the
! include statement is re-evaluated each time it is encountered (and only when it is
! being executed), whereas the <function>require</function> statement is replaced by
! the required file when it is first encountered, whether the contents of the file
! will be evaluated or not (for example, if it is inside an if statement whose
! condition evaluated to false).
<para>
Because <function>include</function> is a special language construct, you
must enclose it within a statement block if it is inside a conditional
block.
<informalexample><programlisting>
/* This is WRONG and will not work as desired. */
if ($condition)
--- 715,743 ----
This happens each time the INCLUDE statement is encountered,
so you can use an INCLUDE statement within a looping structure
to include a number of different file.
+
<informalexample><programlisting>
$files = array('first.inc', 'second.inc', 'third.inc');
for ($i = 0; $i < count($files); $i++) {
include($files[$i]);
}
</programlisting></informalexample>
+
<para>
! <function>include</function> differs from
! <function>require</function> in that the include statement is
! re-evaluated each time it is encountered (and only when it is
! being executed), whereas the <function>require</function>
! statement is replaced by the required file when it is first
! encountered, whether the contents of the file will be evaluated or
! not (for example, if it is inside an if statement whose condition
! evaluated to false).
!
<para>
Because <function>include</function> is a special language construct, you
must enclose it within a statement block if it is inside a conditional
block.
+
<informalexample><programlisting>
/* This is WRONG and will not work as desired. */
if ($condition)
***************
*** 714,723 ****
include($other);
}
</programlisting></informalexample>
<para>
! When the file is evaluated, the parser beings in "HTML-mode" which will
! output the contents of the file until the first PHP start tag (<?)
! is encountered.
<para>
See also <function>readfile</function>, <function>virtual</function>.
--- 752,762 ----
include($other);
}
</programlisting></informalexample>
+
<para>
! When the file is evaluated, the parser beings in "HTML-mode" which
! will output the contents of the file until the first PHP start tag
! (<?) is encountered.
<para>
See also <function>readfile</function>, <function>virtual</function>.
***************
*** 725,730 ****
--- 764,770 ----
<title>FUNCTION</title>
<para>
A function may be defined using syntax such as the following:
+
<informalexample>
<programlisting>
function foo( $arg_1, $arg_2, ..., $arg_n ) {
***************
*** 746,751 ****
--- 786,792 ----
<para>
Values are returned by using the optional return statement. Any
type may be returned, including lists and objects.
+
<informalexample>
<programlisting>
function my_sqrt( $num ) {
***************
*** 758,763 ****
--- 799,805 ----
<para>
Multiple values may not be returned, but the same effect can be
achieved by returning a list:
+
<informalexample>
<programlisting>
function foo() {
***************
*** 778,786 ****
<para>
PHP3 supports passing arguments by value (the default), <link
linkend="keyword.function.arguments.reference">passing by
! reference</link>, and <link
linkend="keyword.function.arguments.default">default argument
! values</link>. Variable-length argument lists are not supported,
but a similar effect may be achieved by passing arrays.
<informalexample><programlisting>
function takes_array($input) {
echo "$input[0] + $input[1] = ", $input[0]+$input[1];
--- 820,830 ----
<para>
PHP3 supports passing arguments by value (the default), <link
linkend="keyword.function.arguments.reference">passing by
! reference</link>, and <link
! linkend="keyword.function.arguments.default">default argument
! values</link>. Variable-length argument lists are not supported,
but a similar effect may be achieved by passing arrays.
+
<informalexample><programlisting>
function takes_array($input) {
echo "$input[0] + $input[1] = ", $input[0]+$input[1];
***************
*** 789,794 ****
--- 833,839 ----
<sect3 id="keyword.function.arguments.reference">
<title>Passing by reference</title>
+
<simpara>
By default, function arguments are passed by value. If you wish
to allow a function to modify its arguments, you may pass them
***************
*** 798,807 ****
If you wish a function's argument to always be passed by
reference, you can prepend an ampersand (&) to the argument
name in the function definition:
<informalexample>
<programlisting>
function foo( &$bar ) {
! $bar .= ' and something extra.';
}
$str = 'This is a string, ';
foo2( $str );
--- 843,853 ----
If you wish a function's argument to always be passed by
reference, you can prepend an ampersand (&) to the argument
name in the function definition:
+
<informalexample>
<programlisting>
function foo( &$bar ) {
! $bar .= ' and something extra.';
}
$str = 'This is a string, ';
foo2( $str );
***************
*** 813,822 ****
If you wish to pass a variable by reference to a function which
does not do this by default, you may prepend an ampersand to the
argument name in the function call:
<informalexample>
<programlisting>
function foo( $bar ) {
! $bar .= ' and something extra.';
}
$str = 'This is a string, ';
foo2( $str );
--- 859,869 ----
If you wish to pass a variable by reference to a function which
does not do this by default, you may prepend an ampersand to the
argument name in the function call:
+
<informalexample>
<programlisting>
function foo( $bar ) {
! $bar .= ' and something extra.';
}
$str = 'This is a string, ';
foo2( $str );
***************
*** 836,842 ****
<informalexample>
<programlisting>
function makecoffee( $type = "cappucino" ) {
! echo "Making a cup of $type.\n";
}
echo makecoffee();
echo makecoffee( "espresso" );
--- 883,889 ----
<informalexample>
<programlisting>
function makecoffee( $type = "cappucino" ) {
! echo "Making a cup of $type.\n";
}
echo makecoffee();
echo makecoffee( "espresso" );
***************
*** 850,867 ****
Making a cup of espresso.
</screen>
! <simpara>
! The default value must be a constant expression, not (for
! example) a variable or class member.
<para>
Note that when using default arguments, any defaults should be on
the right side of any non-default arguments; otherwise, things
will not work as expected. Consider the following code snippet:
<informalexample>
<programlisting>
function makeyogurt( $type = "acidophilus", $flavour ) {
! return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt( "raspberry" ); // won't work as expected
</programlisting>
--- 897,915 ----
Making a cup of espresso.
</screen>
! <simpara>
! The default value must be a constant expression, not (for
! example) a variable or class member.
<para>
Note that when using default arguments, any defaults should be on
the right side of any non-default arguments; otherwise, things
will not work as expected. Consider the following code snippet:
+
<informalexample>
<programlisting>
function makeyogurt( $type = "acidophilus", $flavour ) {
! return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt( "raspberry" ); // won't work as expected
</programlisting>
***************
*** 870,876 ****
<para>
The output of the above example is:
<screen>
! Warning: Missing argument 2 in call to makeyogurt() in
/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
Making a bowl of raspberry .
</screen>
--- 918,925 ----
<para>
The output of the above example is:
<screen>
! Warning: Missing argument 2 in call to makeyogurt() in
! /usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
Making a bowl of raspberry .
</screen>
***************
*** 879,885 ****
<informalexample>
<programlisting>
function makeyogurt( $flavour, $type = "acidophilus" ) {
! return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt( "raspberry" ); // works as expected
</programlisting>
--- 928,934 ----
<informalexample>
<programlisting>
function makeyogurt( $flavour, $type = "acidophilus" ) {
! return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt( "raspberry" ); // works as expected
</programlisting>
***************
*** 920,945 ****
A class is a collection of variables and functions working with
these variables. A class is defined using the following syntax:
! <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>
--- 969,996 ----
A class is a collection of variables and functions working with
these variables. A class is defined using the following syntax:
! <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>
***************
*** 973,986 ****
the extends keyword.
</para>
! <informalexample>
! <programlisting role=php>
class Named_Cart extends Cart {
! var $owner;
! function set_owner($name) {
! $this->owner = $name;
! }
}
</programlisting>
</informalexample>
--- 1024,1037 ----
the extends keyword.
</para>
! <informalexample>
! <programlisting role=php>
class Named_Cart extends Cart {
! var $owner;
! function set_owner($name) {
! $this->owner = $name;
! }
}
</programlisting>
</informalexample>
***************
*** 993,1000 ****
normal cart functions on named carts:
</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
--- 1044,1051 ----
normal cart functions on named carts:
</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
***************
*** 1014,1020 ****
becomes a constructur when it has the same name as the class.
</para>
<informalexample>
! <programlisting role=php>
class Auto_Cart extends Cart {
function Auto_Cart() {
$this->add_item("10", 1);
--- 1065,1071 ----
becomes a constructur when it has the same name as the class.
</para>
<informalexample>
! <programlisting role=php>
class Auto_Cart extends Cart {
function Auto_Cart() {
$this->add_item("10", 1);
***************
*** 1024,1037 ****
</informalexample>
<para>
! This defines a class Auto_Cart that is a Cart plus a constructor which
! initializes the cart with one item of article number "10" each time
! a new Auto_Cart is being made with "new". Constructors
! can also take arguments and these arguments can be optional, which
! makes them much more useful.
</para>
<informalexample>
! <programlisting role=php>
class Constructor_Cart {
function Constructor_Cart($item = "10", $num = 1) {
$this->add_item($item, $num);
--- 1075,1088 ----
</informalexample>
<para>
! This defines a class Auto_Cart that is a Cart plus a constructor
! which initializes the cart with one item of article number "10"
! each time a new Auto_Cart is being made with "new". Constructors
! can also take arguments and these arguments can be optional, which
! makes them much more useful.
</para>
<informalexample>
! <programlisting role=php>
class Constructor_Cart {
function Constructor_Cart($item = "10", $num = 1) {
$this->add_item($item, $num);
***************
*** 1039,1047 ****
--- 1090,1100 ----
}
// Shop the same old boring stuff.
+
$default_cart = new Constructor_Cart;
// Shop for real...
+
$different_cart = new Constructor_Cart("20", 17);
</programlisting>
</informalexample>
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net