php - I would like to know how to keep an inventory variable in a class the same for all new objects? -


the problem having keeping inventory variable keep changes made different objects. example, $me object buys 4 items, deducts inventory , leaves 6 in inventory good. make new object $l, inventory starts 10 again, instead of new current inventory of 6.

below php code class

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;     } } 

below normal code

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; 

when create new instance of class (that happens whenever use keyword new) create brand new kind of 'copy' or 'instance' of class. so, when subtract inventory amount, subtracting instance.

php has keyword static change public instance variable of $inventory kind of shared variable between instances of class.

update public static this:

public static $inventory = 10; 

but need several other changes because cannot reference static variable in same way instance variable.

basically need change $this-> self:: in inventory method:

public function inventory($quantity) {     self::$inventory -= $quantity; } 

then when reference static variable instance variable need access this:

echo $me::$inventory; 

your final code this:

<? 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;     } } 

and when call it:

$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; 

here updated class gives more extended idea of how can use objects. instead of inventory class, breaks down item individual class , object can use.

when item scanned loops through inventory items , if there not enough of item in stock return false - in real world scenario, handle error differently case okay. might add method called 'isiteminstock()' inventory class check if available first.

so inventory object instance shared between other objects items in stock. instead of adding/subtracting totals during scan process there gettotal() method recalculate total discount.

<?  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 $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 $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 $item) {     echo $item->name . ': '. $item->quantity." in stock\n"; }  echo "scanning 6 news\n"; $cb1->scan( 'news', 5);  foreach ($inventory->items $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 $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"; 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -