torben Thu Feb 22 16:43:23 2001 EDT
Modified files:
/phpdoc/en/language oop.xml
Log:
Added an example to help clarify the lack of non-constant variable
initializers in classes in PHP 4 (spurred by Bug #9414).
Index: phpdoc/en/language/oop.xml
diff -u phpdoc/en/language/oop.xml:1.6 phpdoc/en/language/oop.xml:1.7
--- phpdoc/en/language/oop.xml:1.6 Fri Feb 9 11:41:08 2001
+++ phpdoc/en/language/oop.xml Thu Feb 22 16:43:23 2001
@@ -40,10 +40,38 @@
array of articles in the cart and two functions to add and remove
items from this cart.
/para
- notesimpara
- In PHP 4, only constant initializers for literalvar/literal
- variables are allowed. Use constructors for non-constant initializers.
- /simpara/note
+
+ note
+simpara
+ In PHP 4, only constant initializers for literalvar/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 @@
informalexample
programlisting role="php"
- $cart = new Cart;
- $cart-add_item("10", 1);
+$cart = new Cart;
+$cart-add_item("10", 1);
/programlisting
/informalexample