0

I'm pretty new in PHP, I started to learn OOP, and I'm trying to learn association, however, my code isn't working, I really don't know why, but the problem is on the line 38, when I call the method Driving, it says that must be one instance of Person, what it means?

Src:

<?php

class Person
{
    private $name;

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

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

class Car
{

    private $size;
    private $model;

    public function __construct($size, $model)
    {
        $this->size = $size;
        $this->model = $model;
    }

    public function Driving(Person $person)
    {
        echo "The size is: {$this->size}<br>";
        echo "Look at the model of the car: " . $this->model;
        echo "Look at the name of the person: " . $person->getName();
    }
}
$obj1 = new Person("Someone");
$obj2 = new Car("120", "Nothing");
$obj2->Driving($person);

?>

1 Answers1

1

Look at your code:

$obj1 = new Person("Someone");
$obj2 = new Car("120", "Nothing");
$obj2->Driving($person);

You used $person but the person object is $obj2.

Use $obj2 instead of $person or rename $obj2 to $person.

dan1st
  • 12,568
  • 8
  • 34
  • 67