Cleaning up here also.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@17145 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Egon Schmid 1999-12-13 18:43:44 +00:00
parent 69fa8269a5
commit 85d5f213ec
2 changed files with 322 additions and 304 deletions

View file

@ -1,57 +1,56 @@
<chapter id="functions">
<title>Functions</title>
<chapter id="functions">
<title>Functions</title>
<sect1 id="functions.user-defined">
<title>User-defined functions</title>
<sect1 id="functions.user-defined">
<title>User-defined functions</title>
<para>
A function may be defined using syntax such as the following:
<para>
A function may be defined using syntax such as the following:
<informalexample>
<programlisting>
<informalexample>
<programlisting role="php">
function foo ($arg_1, $arg_2, ..., $arg_n) {
echo "Example function.\n";
return $retval;
}
</programlisting>
</informalexample></para>
<simpara>
Any valid PHP code may appear inside a function, even other
functions and <link linkend="keyword.class">class</link>
definitions.</simpara>
</programlisting>
</informalexample>
</para>
<simpara>
In PHP3, functions must be defined before they are referenced. No
such requirement exists in PHP4.
Any valid PHP code may appear inside a function, even other
functions and <link linkend="keyword.class">class</link>
definitions.
</simpara>
<simpara>
PHP does not support function overloading, nor is it possible to
undefine or redefine previously-declared functions.
In PHP3, functions must be defined before they are referenced. No
such requirement exists in PHP4.
</simpara>
<simpara>
PHP3 does not support variable numbers of arguments to functions,
although default arguments are supported (see <link
linkend="functions.arguments.default">Default argument
values</link> for more information). PHP4 supports both: see <link
linkend="functions.variable-arg-list">Variable-length argument
lists</link> and the function references for
<function>func_num_args</function>,
<function>func_get_arg</function>, and
<function>func_get_args</function> for more information.
PHP does not support function overloading, nor is it possible to
undefine or redefine previously-declared functions.
</simpara>
<simpara>
PHP3 does not support variable numbers of arguments to functions,
although default arguments are supported (see <link
linkend="functions.arguments.default">Default argument
values</link> for more information). PHP4 supports both: see <link
linkend="functions.variable-arg-list">Variable-length argument
lists</link> and the function references for
<function>func_num_args</function>,
<function>func_get_arg</function>, and
<function>func_get_args</function> for more information.
</simpara>
</sect1>
<sect1 id="functions.arguments">
<title>Function arguments</title>
<simpara>
Information may be passed to functions via the argument list,
which is a comma-delimited list of variables and/or constants.</simpara>
<sect1 id="functions.arguments">
<title>Function arguments</title>
<simpara>
Information may be passed to functions via the argument list,
which is a comma-delimited list of variables and/or constants.
</simpara>
<para>
PHP supports passing arguments by value (the default), <link
linkend="functions.arguments.by-reference">passing by
@ -67,47 +66,49 @@ function foo ($arg_1, $arg_2, ..., $arg_n) {
similar effect can be achieved in PHP3 by passing an array of
arguments to a function:
<informalexample>
<programlisting>
<informalexample>
<programlisting role="php">
function takes_array($input) {
echo "$input[0] + $input[1] = ", $input[0]+$input[1];
}
</programlisting>
</informalexample></para>
</programlisting>
</informalexample>
</para>
<sect2 id="functions.arguments.by-reference">
<title>Making arguments be passed by reference</title>
<sect2 id="functions.arguments.by-reference">
<title>Making arguments be passed by reference</title>
<simpara>
By default, function arguments are passed by value (so that
if you change the value of the argument within the function,
it does not get changed outside of the function). If you wish
to allow a function to modify its arguments, you must pass them
by reference.</simpara>
<para>
If you want an argument to a function to always be passed by
reference, you can prepend an ampersand (&amp;) to the argument
name in the function definition:
<simpara>
By default, function arguments are passed by value (so that if
you change the value of the argument within the function, it does
not get changed outside of the function). If you wish to allow a
function to modify its arguments, you must pass them by
reference.
</simpara>
<para>
If you want an argument to a function to always be passed by
reference, you can prepend an ampersand (&amp;) to the argument
name in the function definition:
<informalexample>
<programlisting>
<informalexample>
<programlisting role="php">
function add_some_extra(&amp;$string) {
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // outputs 'This is a string, and something extra.'
</programlisting>
</informalexample></para>
</programlisting>
</informalexample>
</para>
<para>
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:
<para>
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>
<informalexample>
<programlisting role="php">
function foo ($bar) {
$bar .= ' and something extra.';
}
@ -116,199 +117,214 @@ foo ($str);
echo $str; // outputs 'This is a string, '
foo (&amp;$str);
echo $str; // outputs 'This is a string, and something extra.'
</programlisting>
</informalexample></para></sect2>
</programlisting>
</informalexample>
</para>
</sect2>
<sect2 id="functions.arguments.default">
<title>Default argument values</title>
<sect2 id="functions.arguments.default">
<title>Default argument values</title>
<para>
A function may define C++-style default values for scalar
arguments as follows:
<para>
A function may define C++-style default values for scalar
arguments as follows:
<informalexample>
<programlisting>
<informalexample>
<programlisting role="php">
function makecoffee ($type = "cappucino") {
return "Making a cup of $type.\n";
}
echo makecoffee ();
echo makecoffee ("espresso");
</programlisting>
</informalexample></para>
</programlisting>
</informalexample>
</para>
<para>
The output from the above snippet is:
<para>
The output from the above snippet is:
<screen>
<screen>
Making a cup of cappucino.
Making a cup of espresso.
</screen></para>
</screen>
</para>
<simpara>
The default value must be a constant expression, not (for
example) a variable or class member.</simpara>
<para>
In PHP 4.0 it's also possible to specify <literal>unset</literal>
for default argument. This means that the argument will not be
set at all, if a value is not supplied.</para>
<simpara>
The default value must be a constant expression, not (for
example) a variable or class member.
</simpara>
<para>
In PHP 4.0 it's also possible to specify <literal>unset</literal>
for default argument. This means that the argument will not be
set at all, if a value is not supplied.
</para>
<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:
<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>
<informalexample>
<programlisting role="php">
function makeyogurt ($type = "acidophilus", $flavour) {
return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt ("raspberry"); // won't work as expected
</programlisting>
</informalexample></para>
</programlisting>
</informalexample>
</para>
<para>
The output of the above example is:
<para>
The output of the above example is:
<screen>
<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></para>
</screen>
</para>
<para>
Now, compare the above with this:
<para>
Now, compare the above with this:
<informalexample>
<programlisting>
<informalexample>
<programlisting role="php">
function makeyogurt ($flavour, $type = "acidophilus") {
return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt ("raspberry"); // works as expected
</programlisting>
</informalexample></para>
</programlisting>
</informalexample>
</para>
<para>
The output of this example is:
<para>
The output of this example is:
<screen>
<screen>
Making a bowl of acidophilus raspberry.
</screen>
</para>
</para>
</sect2>
<sect2 id="functions.variable-arg-list">
<title>Variable-length argument lists</title>
<title>Variable-length argument lists</title>
<simpara>
PHP4 has support for variable-length argument lists in
user-defined functions. This is really quite easy, using the
<function>func_num_args</function>,
<function>func_get_arg</function>, and
<function>func_get_args</function> functions.
</simpara>
<simpara>
PHP4 has support for variable-length argument lists in
user-defined functions. This is really quite easy, using the
<function>func_num_args</function>,
<function>func_get_arg</function>, and
<function>func_get_args</function> functions.
</simpara>
<simpara>
No special syntax is required, and argument lists may still be
explicitly provided with function definitions and will behave as
normal.
</simpara>
<simpara>
No special syntax is required, and argument lists may still be
explicitly provided with function definitions and will behave as
normal.
</simpara>
</sect2>
</sect1>
<sect1 id="functions.returning-values">
<title>Returning values</title>
<sect1 id="functions.returning-values">
<title>Returning values</title>
<para>
Values are returned by using the optional return statement. Any
type may be returned, including lists and objects.
<para>
Values are returned by using the optional return statement. Any
type may be returned, including lists and objects.
<informalexample>
<programlisting>
<informalexample>
<programlisting role="php">
function square ($num) {
return $num * $num;
}
echo square (4); // outputs '16'.
</programlisting>
</informalexample></para>
</programlisting>
</informalexample>
</para>
<para>
You can't return multiple values from a function, but similar
results can be obtained by returning a list.
<para>
You can't return multiple values from a function, but similar
results can be obtained by returning a list.
<informalexample>
<programlisting>
<informalexample>
<programlisting role="php">
function small_numbers() {
return array (0, 1, 2);
return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
</programlisting>
</informalexample></para></sect1>
</programlisting>
</informalexample>
</para>
</sect1>
<sect1 id="functions.old-syntax">
<title><literal>old_function</literal></title>
<sect1 id="functions.old-syntax">
<title><literal>old_function</literal></title>
<simpara>
The <literal>old_function</literal> statement allows you to declare
a function using a syntax identical to PHP/FI2 (except you must
replace 'function' with 'old_function'.</simpara>
<simpara>
This is a deprecated feature, and should only be used by the
PHP/FI2->PHP3 convertor.</simpara>
<warning>
<para>
Functions declared as <literal>old_function</literal>
cannot be called from PHP's internal code. Among other
things, this means you can't use them in functions such as
<function>usort</function>, <function>array_walk</function>, and
<function>register_shutdown_function</function>. You can get around
this limitation by writing a wrapper function (in normal PHP3 form)
to call the <literal>old_function</literal>.
</para>
</warning>
<simpara>
The <literal>old_function</literal> statement allows you to
declare a function using a syntax identical to PHP/FI2 (except you
must replace 'function' with 'old_function'.
</simpara>
<simpara>
This is a deprecated feature, and should only be used by the
PHP/FI2->PHP3 convertor.
</simpara>
<warning>
<para>
Functions declared as <literal>old_function</literal> cannot be
called from PHP's internal code. Among other things, this means
you can't use them in functions such as
<function>usort</function>, <function>array_walk</function>, and
<function>register_shutdown_function</function>. You can get
around this limitation by writing a wrapper function (in normal
PHP3 form) to call the <literal>old_function</literal>.
</para>
</warning>
</sect1>
<sect1 id="functions.variable-functions">
<title>Variable functions</title>
<para>
PHP supports the concept of variable functions. This means that if
a variable name has parentheses appended to it, PHP will look for
a function with the same name as whatever the variable evaluates
to, and will attempt to execute it. Among other things, this can
be used to implement callbacks, function tables, and so forth.
PHP supports the concept of variable functions. This means that if
a variable name has parentheses appended to it, PHP will look for
a function with the same name as whatever the variable evaluates
to, and will attempt to execute it. Among other things, this can
be used to implement callbacks, function tables, and so forth.
</para>
<para>
<example>
<title>Variable function example</title>
<programlisting>
<example>
<title>Variable function example</title>
<programlisting role="php">
&lt;?php
function foo() {
echo "In foo()&lt;br&gt;\n";
echo "In foo()&lt;br>\n";
}
function bar( $arg = '' ) {
echo "In bar(); argument was '$arg'.&lt;br&gt;\n";
echo "In bar(); argument was '$arg'.&lt;br>\n";
}
$func = 'foo';
$func();
$func = 'bar';
$func( 'test' );
?&gt;
</programlisting>
</example>
?>
</programlisting>
</example>
</para>
</sect1>
</chapter>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
@ -320,7 +336,7 @@ $func( 'test' );
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../manual.ced"
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil

View file

@ -1,152 +1,154 @@
<chapter id="oop">
<title>Classes and Objects</title>
<chapter id="oop">
<title>Classes and Objects</title>
<sect1 id="keyword.class">
<title><literal>class</literal></title>
<para>
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">
&lt;?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>
This defines a class named Cart that consists of an associative
array of articles in the cart and two functions to add and remove
items from this cart.
</para><para>
Classes are types, that is, they are blueprints for actual
variables. You have to create a variable of the desired type with
the new operator.
</para>
<sect1 id="keyword.class">
<title><literal>class</literal></title>
<para>
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">
&lt;?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>
This defines a class named Cart that consists of an associative
array of articles in the cart and two functions to add and remove
items from this cart.
</para>
<para>
Classes are types, that is, they are blueprints for actual
variables. You have to create a variable of the desired type with
the new operator.
</para>
<informalexample>
<programlisting role="php">
$cart = new Cart;
$cart->add_item("10", 1);
</programlisting>
</informalexample>
</programlisting>
</informalexample>
<para>
This creates an object $cart of the class Cart. The function
add_item() of that object is being called to add 1 item of article
number 10 to the cart.
</para><para>
Classes can be extensions of other classes. The extended or
derived class has all variables and functions of the base class
and what you add in the extended definition. This is done using
the extends keyword. Multiple inheritance is not supported.
</para>
<para>
This creates an object $cart of the class Cart. The function
add_item() of that object is being called to add 1 item of article
number 10 to the cart. </para><para> Classes can be extensions of
other classes. The extended or derived class has all variables and
functions of the base class and what you add in the extended
definition. This is done using the extends keyword. Multiple
inheritance is not supported.
</para>
<informalexample>
<programlisting role="php">
class Named_Cart extends Cart {
var $owner;
<informalexample>
<programlisting role="php">
class Named_Cart extends Cart {
var $owner;
function set_owner ($name) {
$this->owner = $name;
}
}
</programlisting>
</informalexample>
function set_owner ($name) {
$this->owner = $name;
}
}
</programlisting>
</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:
</para>
<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:
</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>
<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>
Within functions of a class the variable $this means this
object. You have to use $this->something to access any variable or
function named something within your current object.
</para>
<para>
Within functions of a class the variable $this means this
object. You have to use $this->something to access any variable or
function named something within your current object.
</para>
<para>
Constructors are functions in a class that are automatically
called when you create a new instance of a class. A function
becomes a constructor when it has the same name as the class.
</para>
<para>
Constructors are functions in a class that are automatically
called when you create a new instance of a class. A function
becomes a constructor 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);
}
}
</programlisting>
</informalexample>
<informalexample>
<programlisting role="php">
class Auto_Cart extends Cart {
function Auto_Cart () {
$this->add_item ("10", 1);
}
}
</programlisting>
</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>
<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 extends Cart {
function Constructor_Cart ($item = "10", $num = 1) {
$this->add_item ($item, $num);
}
}
<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.
// Shop the same old boring stuff.
$default_cart = new Constructor_Cart;
$default_cart = new Constructor_Cart;
// Shop for real...
// Shop for real...
$different_cart = new Constructor_Cart ("20", 17);
</programlisting>
</informalexample>
$different_cart = new Constructor_Cart ("20", 17);
</programlisting>
</informalexample>
<caution>
<simpara>
For derived classes, the constructor of the parent class is not
automatically called when the derived class's constructor is
called.
</simpara>
</caution>
</sect1>
<caution>
<simpara>
For derived classes, the constructor of the parent class is not
automatically called when the derived class's constructor is called.</simpara>
</caution></sect1>
</chapter>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
@ -158,7 +160,7 @@
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../manual.ced"
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil