Jump to content

problem with OOP


drtanz

Recommended Posts

Hi I am trying out some object oriented programming but I am stuck on the following problem. As you can see from the code below I have two classes, Bear and PolarBear, which inherits from Bear. Now when I create a new instance of PolarBear I am getting the food and color attributes of Bear in this instance instead of those defined in PolarBear. What am i doing wrong? thanks

 

Hi, my name is Nikolai and I weigh 235 kgs

Nikolai eats honey

Nikolai is brown

Hi, my name is Jon and I weigh 300 kgs

Jon eats honey

Jon is brown

Hi, my name is Maria and I weigh 100 kgs

Maria eats honey

Maria is brown

Hi, my name is Banavage and I weigh 500 kgs

Banavage eats honey

Banavage is brown

 

 

index.php

 

<?php

include 'bear.cls.php';

include 'polarBear.cls.php';

$nikolai = new Bear('Nikolai',235);

  $jon = new Bear('Jon',300);

$maria = new Bear('Maria',100);

$banavage = new PolarBear('Banavage',500);

?>

 

 

polarBear.cls.php

 

<?php

 

class PolarBear extends Bear {

 

public function PolarBear($bearName,$bearWeight) {

parent::Bear($bearName,$bearWeight);

$this->color = 'white';

$this->food = 'fish';

$this->name = $bearName;

// $this->Eat();

//$this->Color();

}

 

/* public function Eat() {

echo $this->name." eats ".$this->food."<br />";

}

 

public function Color() {

echo $this->name." is ".$this->color."<br />";

}

*/

 

}

 

bear.cls.php

 

<?php

 

class Bear {

public $name;

public $weight;

public $age;

public $color;

public $food;

 

public function Bear($bearName,$bearWeight) {

//echo "Hi, my name is ".$name." and I weigh ".$weight." kgs<br />";

$this->name = $bearName;

$this->weight = $bearWeight;

$this->color = 'brown';

$this->food = 'honey';

$this->NameWeight();

$this->Eat();

$this->Color();

}

 

public function Eat() {

echo $this->name." eats ".$this->food."<br />";

}

 

public function Color() {

echo $this->name." is ".$this->color."<br />";

}

 

public function NameWeight() {

echo "Hi, my name is ".$this->name." and I weigh ".$this->weight." kgs<br />";

}

 

}

 

?>

Link to comment
Share on other sites

You are calling the eat() and color() methods in the Bear constuctor, while those in PolarBear are commented out. Therefore the PolarBear is always Brown and eats Honey since it hasn't set it's color to white or it's food to fish before it's parent's constructor is called. Uncomment the calls in PolarBear and you should see something like:

 

Hi, my name is Banavage and I weigh 500 kgs

Banavage eats honey

Banavage is brown

Banavage eats fish

Banavage is white

Link to comment
Share on other sites

Well, remove those calls from the constructor then

 

It's cleaner to do something like:

 

$nikolai = new Bear('Nikolai',235);
$jon = new Bear('Jon',300);
$maria = new Bear('Maria',100);
$banavage = new PolarBear('Banavage',500);

$nikolai->info();
$jon->info();
$maria->info();
$banavage->info();

 

where the info() method is like:

public function info()
{
    $this->NameWeight();
    $this->Eat();
    $this->Color();
}

 

And as a side note, this belongs in the OOP subforum ::)

Link to comment
Share on other sites

hi sorry for posting here missed the other forum, i can see that this is a cleaner way, im just struggling to understand why this way it works and when they were called from the constructor it didnt work, i might be overseeing something obvious but i cant figure it out yet...

Link to comment
Share on other sites

Just remember that everything inside functions or objects happens sequentially, one thing after the other.

 

The PolarBear constructor goes like this (with the calls uncommented):

 

public function PolarBear($bearName,$bearWeight) 
{
     parent::Bear($bearName,$bearWeight);
     $this->color = 'white';
     $this->food = 'fish';
     $this->name = $bearName;
     $this->Eat();
     $this->Color();
}

 

Notice that BEFORE setting the color to white and food to fish you are calling the parent's (Bear) constructor. So let's do a simple replace to get the equivalent code and see what is going on:

 

public function PolarBear($bearName,$bearWeight) 
{
     /* Begin parent's constructor */
     $this->name = $bearName;
     $this->weight = $bearWeight;
     $this->color = 'brown';
     $this->food = 'honey';
     $this->NameWeight();
     $this->Eat();       //Outputs: Honey
     $this->Color();     //Outputs: Brown
     /* End parent's constructor */

     $this->color = 'white';
     $this->food = 'fish';
     $this->name = $bearName;
     $this->Eat();       //Outputs: Fish
     $this->Color();     //Outputs: White
}

 

You see now? The Bear constructor calls the Eat() and Color() Methods before the PolarBear different properties are defined, thus it can only read the ones that the parent Bear class has set. You can also see that you are setting the same name twice... not really important though.

 

Hope that clears it up some more. Cheers!

Link to comment
Share on other sites

Right thats a great explanation i see what is happenning now, I was just thinking there was some other way of doing it the way I was doing it in the beginning, without having to use the info() function and in some way overriding the Bear's eat and color functions but it seems that wasnt possible. thanks :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.