我对 PHP 还很陌生,我开始学习 OOP,并且正在尝试学习关联,但是,我的代码无法正常工作,我真的不知道为什么,但问题出在第 38 行,当我将方法称为 Driving,它说必须是 Person 的一个实例,这是什么意思?来源:<?phpclass 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 回答
函数式编程
TA贡献1807条经验 获得超9个赞
看看你的代码:
$obj1 = new Person("Someone");
$obj2 = new Car("120", "Nothing");
$obj2->Driving($person);
您使用了$person但 person 对象是$obj2.
使用$obj2代替$person或重命名$obj2为$person.
- 1 回答
- 0 关注
- 84 浏览
添加回答
举报
0/150
提交
取消