0

Okay, here is my print_details function

class Vehicle{
      //constructor goes here 
      public function print_details(//one object as parameter)
      {
            echo "\nName : $this->name";
            echo "\nDescription: $this->desc \n";
            if(strnatcasecmp(get_class($this),"Car")==0)
            {
               $this->getCarDetails();
            }  
            elseif (strnatcasecmp(get_class($this),"Bus")==0)
            {
               $this->getBusDetails();
            }
      }
}  

I intend to use only one object as a parameter, which can be of either class Car or Bus. But it should call the appropriate function based on the class of the object.

Is it possible to do it? If yes,how?

Barmar
  • 741,623
  • 53
  • 500
  • 612
b.g
  • 411
  • 1
  • 3
  • 19
  • just put a variable inside the argument, then add an if else checking if its an _`instanceof`_ that class that you want to check – Kevin Sep 01 '16 at 05:05
  • 1
    Why are you doing it this way? The whole point of OOP is to let the system dispatch automatically. The classes should both have a `getDetails()` method. – Barmar Sep 01 '16 at 05:06
  • Otherwise, every time you add a subclass, you're going to have to add another case to your `if`. – Barmar Sep 01 '16 at 05:07
  • @Barmar The basic variables $name and $desc reside in the parent class Vechicle. The child class Car and Bus store other details. So I decided to put the function to print details in Parent class itself, which would then appropriately call the required functions. – b.g Sep 01 '16 at 05:40
  • It's fine to have `print_details` in the parent class. But it should then call `$this->getDetails()` to get the details of the child class. – Barmar Sep 01 '16 at 05:48
  • Possible duplicate of [What is polymorphism, what is it for, and how is it used?](http://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used) – Alexander Guz Sep 01 '16 at 11:18
  • @b.g you are doing the OOP wrong. I would recommend you do do some extend reading on the subject. Learn out what the point of polymorphism is, learn about inheritance, learn about protected method and variables, learn about interface segregation, etc. – tereško Sep 01 '16 at 19:43
  • 1
    You might want to look at interfaces (contracts): http://php.net/manual/en/language.oop5.interfaces.php – Nitin Sep 01 '16 at 19:44

1 Answers1

0

I would suggest you to use the following class structures:

abstract class Vehicle {

    protected $name;
    protected $desc;

    abstract public function getDetails();

    //constructor goes here
    public function print_details()
    {
        echo "Name : $this->name", PHP_EOL;
        echo "Description: $this->desc", PHP_EOL;

        foreach ($this->getDetails() as $key => $value) {
            echo "{$key}: {$value}", PHP_EOL;
        }
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getDesc()
    {
        return $this->desc;
    }

    public function setDesc($desc)
    {
        $this->desc = $desc;
    }
}

class Car extends Vehicle {

    protected $type;

    public function getType()
    {
        return $this->type;
    }

    public function setType($type)
    {
        $this->type = $type;
    }

    public function getDetails()
    {
        return [
            'Type' => $this->type
        ];
    }

}

class Bus extends Vehicle {

    protected $numberOfSeats;

    /**
     * @return mixed
     */
    public function getNumberOfSeats()
    {
        return $this->numberOfSeats;
    }

    /**
     * @param mixed $numberOfSeats
     */
    public function setNumberOfSeats($numberOfSeats)
    {
        $this->numberOfSeats = $numberOfSeats;
    }

    public function getDetails()
    {
        return [
            'Number of seats' => $this->numberOfSeats
        ];
    }
}

$car = new Car();
$car->setName('BMW');
$car->setDesc('Car description');
$car->setType('sedan');
$car->print_details();

$car = new Bus();
$car->setName('Mers');
$car->setDesc('Bus description');
$car->setNumberOfSeats(20);
$car->print_details();
tereško
  • 58,060
  • 25
  • 98
  • 150
Andrej
  • 7,474
  • 1
  • 19
  • 21
  • downvote removed, because I made the mistake of just reading the OP's question and not his comment. – tereško Sep 01 '16 at 19:42