gpt4 book ai didi

php - 我想知道如何使所有新对象的类中的库存变量保持相同?

转载 作者:行者123 更新时间:2023-12-02 10:59:07 24 4
gpt4 key购买 nike

我遇到的问题是让我的库存变量跟上不同对象所做的更改。例如,$me 对象购买了 4 件元素,这会从库存中扣除,并在库存中留下 6 件完好的元素。然后我创建了一个新对象 $l,但库存又从 10 开始,而不是新的当前库存 6。

下面是我的类(class)的 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)实例变量更改为该类的所有实例之间的共享变量。

将其从公共(public)更新为静态,如下所示:

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 - 在现实场景中,您可能会以不同的方式处理该错误,但对于这种情况,这是可以的。您可以向 Inventory 类添加一个名为“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 - 我想知道如何使所有新对象的类中的库存变量保持相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34824085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com