问题在于保持我的库存变量跟上不同对象所做的更改。例如,$ me对象购买4个项目,该项目从库存中扣除,并将6个存留在良好的库存中。然后,我创建一个新对象$ l,但是库存又从10开始,而不是从当前的新库存6。
以下是我的课程的PHP代码
class cashRegister {
public $total = 0;
public $name;
public $discount;
public $lastamount;
public $inventory = 10;
public function __construct($name, $discount) {
$this->name = $name;
$this->discount = $discount;
}
public function add($itemCost) {
$this->total += $itemCost;
$this->lastamount = $itemCost;
}
public function scan($item, $quantity) {
switch ($item) {
case "eggs" :
$this->add ( 1 * $quantity);
$this->inventory($quantity);
break;
case "news" :
$this->add(2 * $quantity);
$this->inventory($quantity);
}
//$this->inventory -= $quantity;
}
public function inventory($quantity) {
$this->inventory -= $quantity;
}
public function staffDiscount() {
$this->total -= ($this->total * ($this->discount/100)) ;
}
public function voidL() {
$this->total -= $this->lastamount;
}
}
下面是我的正常代码
include 'cashRegister.php';
$me = new cashRegister("Mg", 20);
$l = new cashRegister("ll", 50);
echo $me->inventory;
$me->scan("eggs", 2);
$me->scan("eggs", 1);
$me->scan("news", 1);
//$me->staffDiscount();
echo $me->inventory;
//echo $l->inventory;
//echo $me->total;
参考方案
创建类的新实例时(每次使用关键字new
都会发生),它将创建该类的全新“复制”或“实例”。因此,当您减去库存量时,仅从该实例中减去。
PHP具有关键字static
,它将把$ inventory的公共实例变量更改为该类所有实例之间的一种共享变量。
从公共更新为静态,如下所示:
public static $inventory = 10;
但是您还需要进行其他一些更改,因为您不能以与实例变量相同的方式引用静态变量。
基本上,您需要在清单方法中将$this->
更改为self::
:
public function inventory($quantity) {
self::$inventory -= $quantity;
}
然后,当您从实例变量引用静态变量时,您需要像这样访问它:
echo $me::$inventory;
您的最终代码如下所示:
<?
class cashRegister {
public $total = 0;
public $name;
public $discount;
public $lastamount;
public static $inventory = 10;
public function __construct($name, $discount) {
$this->name = $name;
$this->discount = $discount;
}
public function add($itemCost) {
$this->total += $itemCost;
$this->lastamount = $itemCost;
}
public function scan($item, $quantity) {
switch ($item) {
case "eggs" :
$this->add ( 1 * $quantity);
$this->inventory($quantity);
break;
case "news" :
$this->add(2 * $quantity);
$this->inventory($quantity);
}
//$this->inventory -= $quantity;
}
public function inventory($quantity) {
self::$inventory -= $quantity;
}
public function staffDiscount() {
$this->total -= ($this->total * ($this->discount/100)) ;
}
public function voidL() {
$this->total -= $this->lastamount;
}
}
当您调用它时:
$me = new cashRegister("Mg", 20);
$l = new cashRegister("ll", 50);
echo $me::$inventory;
echo "<br>";
$me->scan("eggs", 2);
$me->scan("eggs", 1);
$me->scan("news", 1);
//$me->staffDiscount();
echo $me::$inventory;
//echo $l->inventory;
//echo $me->total;
这是一个更新的类,为您提供了有关如何使用对象的更多扩展思路。
它不仅将清单类分解为可用的单个类和对象。
扫描物料时,它将循环遍历库存物料,如果库存中的物料不足,它将返回false-在实际情况下,您可能会以不同的方式处理该错误,但是在这种情况下可以。您可以在库存类中添加一个名为“ isItemInStock()”的方法,该方法将首先检查该方法是否可用。
因此,既然库存是一个对象,则其实例在其他对象以及库存项目之间共享。不用在扫描过程中添加/减去总数,而是有一个getTotal()方法,该方法将使用折扣重新计算总数。
<?
class Item {
public $name;
public $cost;
public $quantity;
public function __construct($name, $cost, $quantity=null) {
$this->name = $name;
$this->cost = $cost;
$this->quantity = $quantity;
}
}
class Inventory
{
public $items = array();
public function __construct()
{
}
public function add($item) {
$this->items[] = $item;
}
}
class CashRegister {
public $name;
public $discount;
public $inventory;
public $items = array();
public function __construct($name, $discount, $inventory) {
$this->name = $name;
$this->discount = $discount;
$this->inventory = $inventory;
}
public function add($item) {
$this->items[] = $item;
}
public function scan( $name, $qty ) {
foreach ($this->inventory->items as $key => $item) {
if ($item->name==$name) {
if (($item->quantity-$qty)>=0) {
$this->inventory->items[$key]->quantity -= $qty;
$this->add( new Item($item->name, $item->cost, $qty) );
return true;
} else {
// Not added, not enough
return false;
}
}
}
// Invalid item
return false;
}
public function getTotal() {
$total = 0;
foreach ($this->items as $item) {
$total += $item->cost*$item->quantity;
}
$discount = ((100-$this->discount)/100);
echo "Discount total: $discount\n";
return $total - $discount;
}
}
$inventory = new Inventory();
$inventory->add( new Item('eggs', 1, 20) );
$inventory->add( new Item('news', 2, 50) );
$cb1 = new CashRegister(1, 20, $inventory );
echo "<pre>\n";
echo "Scanning 5 eggs\n";
$cb1->scan( 'eggs', 5);
foreach ($inventory->items as $item) {
echo $item->name . ': '. $item->quantity." in stock\n";
}
echo "Scanning 6 news\n";
$cb1->scan( 'news', 5);
foreach ($inventory->items as $item) {
echo $item->name . ': '. $item->quantity." in stock\n";
}
$cb2 = new CashRegister(2, 30, $inventory );
echo "Scanning 3 eggs\n";
$cb2->scan('eggs', 3);
foreach ($inventory->items as $item) {
echo $item->name . ': '. $item->quantity." in stock\n";
}
echo "Cash Register 1 Total: " . $cb1->getTotal() . "\n";
echo "Cash Register 2 Total: " . $cb2->getTotal() . "\n";
PHP-复选框组 - php我有一个需要发布的表单复选框组。<input type="checkbox" value="true" checked name="chk0[]"> <input type="checkbox" value="false" name=…
Div单击与单选按钮相同吗? - php有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date…
php:将分钟取整到最近的四分之一小时,然后执行更多操作 - php最初的问题是这样的:取分钟数->转换为四分之一小时-> 1个四分之一小时为1个单位->输出单位我今天整天都在整理页面,几分钟前我的大脑就停止工作了,我只是不知道如何输出单位数量。我知道在此网站上发布问题会有所帮助。因此,用户输入的分钟数(不是小时和分钟,而是数分钟),站点需要输出单位数量。单位是一个刻钟。分钟总是四舍五入到最近的四分之一小时…
php Singleton类实例将在多个会话中保留吗? - php举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…
PHP:如何从JSON数组获取属性? - php我有以下JSON数组:使用PHP,如何从上面的JSON数组中获取geometery->location->lat&lng值?例如(伪代码):<?php $json = { "status": "OK", "results": [ { "types": [ …