我对 PHP 还很陌生,我开始学习 OOP,并且正在尝试学习关联,但是,我的代码无法正常工作,我真的不知道为什么,但问题出在第 38 行,当我将方法称为 Driving,它说必须是 Person 的一个实例,这是什么意思?
来源:
<?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);
?>
函数式编程