PHP ʖ²ἯTH
º/A
Ղ 14. ӫ¶ԏ
Ŀ¼ ¼̳м/TT ¹¹Ԭº¯ʽ:: parent вP»¯¶ԏỰµĶԏ The magic functions __sleep and __wakeup References inside the constructor
?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;}}}?
עҢ
ςæµľ¯¸既½ⶔ PHP4 ӐЧ¡£
The function names __sleep and __wakeup are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them. See below for more information.
?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'; var $items = array("VCR", "TV");} /* This is how it should be done. */ class Cart {var $todays_date; var $name; var $owner; var $items; function Cart() {$this- todays_date = date("Y-m-d"); $this- name = $GLOBALS['firstname']; /* etc... */}}?
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.
?php $cart = new Cart; $cart- add_item("10", 1); $another_cart = new Cart; $another_cart- add_item("0815", 3);
// correct, single $$cart- items = array("10" = 1); // invalid, because $cart- $items becomes $cart- ""$cart- $items = array("10" = 1); // correct, but may or may not be what was intended: // $cart- $myvar becomes $cart- items $myvar = 'items'; $cart- $myvar = array("10" = 1);
º/A
ưµ㼯A
±䁿º¯ʽ
ɏһ¼¶
¼̳м/TT