我對 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 關注
- 108 瀏覽
添加回答
舉報
0/150
提交
取消