Added an example to help clarify the lack of non-constant variable

initializers in classes in PHP 4 (spurred by Bug #9414).


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@41751 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Torben Wilson 2001-02-23 00:43:23 +00:00
parent 6c96a6832e
commit 5d86777a55

View file

@ -40,10 +40,38 @@ class Cart {
array of articles in the cart and two functions to add and remove
items from this cart.
</para>
<note><simpara>
In PHP 4, only constant initializers for <literal>var</literal>
variables are allowed. Use constructors for non-constant initializers.
</simpara></note>
<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>
Classes are types, that is, they are blueprints for actual
variables. You have to create a variable of the desired type with
@ -52,8 +80,8 @@ class Cart {
<informalexample>
<programlisting role="php">
$cart = new Cart;
$cart->add_item("10", 1);
$cart = new Cart;
$cart->add_item("10", 1);
</programlisting>
</informalexample>