how to manipulate private variable between classes

<p>I'm trying to build a stock inventory and cash register simulation, in the code I have 2 class, first is Inventory and second is Cash. Both have their own properties and method, when I'm trying to sell item the _balance property is giving Error undefined _balance and the _balance is still 0. How should I fix it? Just in case this question have been already asked please just give the link don't mark as duplicate. I just can't find it.</p><pre><br /><code><?php<br /> class Inventory {<br /> public $_name;<br /> public $_price;<br /> public $_stock;<br /> public function _addStock($item,$itemIn)<br /> {<br /> $this->_stock += $itemIn;<br /> }<br /> };<br /><br /> $item2 = new Inventory;<br /> $item2->_name = "item2";<br /> $item2->_price = 5000;<br /> $item2->_addStock('item2',100);<br /> $item1 = new Inventory;<br /> $item1->_name = "item1";<br /> $item1->_price = 1000;<br /> $item1->_addStock('item1',100);<br /><br /> class Cash {<br /> private $_balance = 0;<br /> public function _display(Inventory $_name)<br /> {<br /> echo $_name->_price;<br /> }<br /> public function getBalance()<br /> {<br /> echo $this->_balance;<br /> }<br /> public function sell(Inventory $_name, $amount)<br /> { <br /> return $_name->_stock -= $amount;<br /><br /> return $_balance += $_name->_price*$amount;// Error $_balance undifined<br /> } <br /> };<br /> $cash = new Cash;<br /> print_r($item2);<br /> echo "<br/>";<br /> print_r($item1);<br /> echo " <br/>Balance :<br/>";<br /> $cash->getBalance();<br /> echo "<br/>item1 price :<br/>";<br /> $cash->_display($item1);<br /> echo "<br/>item2 price :<br/>";<br /> $cash->_display($item2);<br /> echo "<br/>item1 sold 12 pcs<br/>";<br /> $cash->sell($item1,12);<br /><br /> $cash->sell($item2,12);<br /> echo "<br/>item2 sold 12 pcs <br/>";<br /><br /> echo "<br/> current stock :<br/>";<br /> print_r($item2);<br /> echo "<br/>";<br /> print_r($item1);<br /><br /> echo "<br/> current balance :<br/>";<br /> $cash->getBalance();<br /> ?><br /></code><br /></pre><br /><h2>Click here view more answers <a href="%20http://stackoverflow.com/questions/34525126/how-to-manipulate-private-variable-between-classes">http://ift.tt/1mhPAeU</a></h2>

Nhận xét

Bài đăng phổ biến từ blog này

Align div to bottom of the page in bootstrap

Align elements inside an tag vertically

Javascript is unable to stop page loading