mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 17:08:54 +00:00
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:
parent
6c96a6832e
commit
5d86777a55
1 changed files with 34 additions and 6 deletions
|
@ -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>
|
||||
|
||||
|
|
Loading…
Reference in a new issue