【发布时间】:2017-06-10 11:31:03
【问题描述】:
为什么会出现这个错误?
警告:implode():在第 17 行的 /Applications/XAMPP/xamppfiles/htdocs/basis/php/php.php 中传递的参数无效
index.php:
<?php
require_once 'php.php';
$piet = new Persoon();
$piet->voornaam = 'Piet';
$piet->achternaam = 'Jansen';
echo "De naam is: " . $piet->showNaam();
$piet->addHobby('zeilen');
$piet->addHobby('hardlopen');
echo "<br/> De hobbies van {$piet->showNaam()} zijn: {$piet->showHobbies()}";
?>
php.php
<?php
class Persoon {
public $voornaam = '';
public $achternaam = '';
protected $adres;
protected $hobbies;
public function showNaam() {
return $this->voornaam . ' ' . $this->achternaam;
}
public function addHobby($hobby) {
$hobbies[] = $hobby;
}
public function showHobbies() {
echo implode(', ', $this->hobbies);
}
}
?>
【问题讨论】:
-
implode 第二个参数应该是数组所以改变这一行 $this->hobbies[] = $hobby;
标签: php