PHP3���IJο��ֲ� ���ķ��룺Sadly [email protected] http://wansoft.soim.net | ||
---|---|---|
��һҳ | �����»������ | ��һҳ |
����һϵ�б����ͺ����ļ��ϡ�������������壺
<?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; } } } ?>
���涨����һ����Cart ���࣬���а���һ���������������������cart�����Ӻ�ɾ����Ŀ�ĺ�����
��������,������ʵ�ʱ�����ԭʼģ�͡���Ҫͨ��new ������������һ���������͵ı�����
$cart = new Cart; $cart->add_item("10", 1);
�⽨����һ�� Cart��Ķ���$cart���ö���ĺ���add_item()������������10��� 1��
����Դ�������������õ��������������������ӵ�л�������б����ͺ������������䶨����������Ķ�������Ҫʹ�� extends �ؼ��֡�
class Named_Cart extends Cart { var $owner; function set_owner($name) { $this->owner = $name; } }
���ﶨ����һ����Ϊ Named_Cart �������̳��� Cart�����б����ͺ�����������һ������ $owner��һ������ set_owner()�� �㽨���� named_cart ��ı������ھ�������carts �� owner�ˡ���named_cart����������Ȼ����ʹ��һ��� cart����:
$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)
�����еı��� $this ��˼�ǵ�ǰ�Ķ�������Ҫʹ�� $this->something ����ʽ����ȡ���е�ǰ����ı���������
���еĹ��������㽨��ij������±���ʱ�Զ������õĺ��������к�����һ���ĺ������ǹ�������
class Auto_Cart extends Cart { function Auto_Cart() { $this->add_item("10", 1); } }
���ﶨ��һ���� Auto_Cart ������ Cart�����һ��ÿ��new����ʱ������Ŀ10���б�����ʼ���Ĺ�������������Ҳ�����в�������Щ�����ǿ�ѡ�ģ����ò������ô��ܴ�
class Constructor_Cart { function Constructor_Cart($item = "10", $num = 1) { $this->add_item($item, $num); } } // Shop the same old boring stuff. $default_cart = new Constructor_Cart; // Shop for real... $different_cart = new Constructor_Cart("20", 17);