Hi,
I would like to post the following question related to an inheritance
problem with PHP OO programming :
With an object of a subclass, I call a method of the parentclass in order
to modify an attribute of the parentclass. It does work correctly but later
when I call the display method of the parentclass, I receive the previous
value of the attribute !!!
The code below shows the problem :
The parentclass is "Ressource"
The subclass is "Water"
The attribute of the parentclass is "$volume"
The method to update $volume is "increase"
The method to display the parentclass is "display"
An explanation and a trick to fix the problem would be greatly appreciated.
Thanks !
Bernard
<?php
define('_MAXX', 10);
define('_MAXY', 10);
define('_MAXENERGY', 20);
define('_MAXRESERVEWATER', 20);
define('_MAXVOLUMEWATER', 10);
define('_MAXVOLUMEPLANT', 10);
class Jungle
{
var $jungleobjects; // composition relation = array of $Jungleobject
function Jungle()
{
// Create eaux
for ($i=1; $i<=2; $i++) { //>
$this->jungleobjects[] = new Water($posx = mt_rand(1, _MAXX),
$posy = mt_rand(1, _MAXY),
$volume = mt_rand(1,
_MAXVOLUMEWATER));
}
// Create plantes
for ($i=1; $i<=1; $i++) { //>
$this->jungleobjects[] = new Plant($posx = mt_rand(1, _MAXX),
$posy = mt_rand(1, _MAXY),
$volume = mt_rand(1,
_MAXVOLUMEPLANT));
}
}
function display()
{
foreach ($this->jungleobjects as $jungleobject) {
$jungleobject->display();
}
}
function receiverain($density)
{
foreach ($this->jungleobjects as $jungleobject) {
$jungleobject->receiverain($density);
}
}
} // Jungle
class Jungleobject
{
var $posx;
var $posy;
function Jungleobject($posx, $posy)
{
$this->posx = $posx;
$this->posy = $posy;
}
function display()
{
echo "<br>";
echo "Class : ".get_class($this)."<br>";
echo "Posx : ".$this->posx."<br>";
echo "Posy : ".$this->posy."<br>";
}
function receiverain($density) // Abstract method : should be defined
at sub-class level (not mandatory)
{
echo "Class : ".get_class($this)." Nothing to do with rain"."<br>";
}
function evoluer() {} // Abstract method : should be defined at
sub-class level (not mandatory)
} // Jungleobject
class Ressource extends Jungleobject
{
var $volume;
function Ressource($posx, $posy, $size)
{
$this->Jungleobject($posx, $posy);
$this->volume = $size;
}
function increase($quantity)
{
echo "Class : ".get_class($this)." method : increase
($quantity)"."<br>";
echo "Actual volume : $this->volume"."<br>";
$this->volume += $quantity;
echo "New volume : $this->volume"."<br>";
}
function display()
{
jungleobject::display();
echo "Volume (Ressource) : $this->volume"."<br>";
}
} // Ressource
class Plant extends Ressource
{
function Plant($posx, $posy, $volume)
{
//$this->Ressource($posx, $posy, $volume);
Ressource::Ressource($posx, $posy, $volume);
}
} // Plant
class Water extends Ressource
{
function Water($posx, $posy, $volume)
{
$this->Ressource($posx, $posy, $volume);
}
function receiverain($density)
{
echo "<br>";
echo "Class : ".get_class($this)." Welcome the rain ($density)
!"."<br>";
$this->increase($density);
//Ressource::increase($density);
}
} // Water
class Rain
{
var $jungle;
function Rain($jungle)
{
$this->jungle = $jungle;
}
function fall($density)
{
$this->jungle->receiverain($density);
}
} // Rain
// Main
$myjungle = new Jungle();
$myrain = new Rain($myjungle);
$myjungle->display();
$myrain->fall(10);
$myjungle->display();
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php