我在构建菜单时遇到问题,我想创建一个这样的类:
class leftMenu{
public $items;
function addItem ($num){
for($i = 0; $i < count($num); $i++){
$this->items[$i] += $num;
echo $this->items[$i];
}
}
public function __destruct() {
//echo "'n</body>'n</html>";
}
}
我想像数组一样调用addItem,例如:
$menu = new leftMenu();
$menu->addItem("one", "two", "three"); // Here 1, 2, 3 should be an array
我做不到。。。请帮忙!!!ND
您需要按照以下方式更新代码(我刚刚初始化了变量$items,以及您可以在注释中看到的另一个编辑):
class leftMenu{
public $items = array(); //intialize your variable $items
function addItem ($num){
for($i = 0; $i < count($num); $i++){
$this->items[$i] = $num[$i]; //Edit this line too
echo $this->items[$i];
}
}
public function __destruct() {
//echo "'n</body>'n</html>";
}
}
当你调用它时,向它传递一个数组,如下所示:
$menu = new leftMenu();
$menu->addItem(array("one", "two", "three")); // Here 1, 2, 3 should be an array
$items = array("one", "two", "three");
$menu->addItems($items);
function addItems($items = array()) {
$this->items = $items;
}
您需要将变量作为数组传入。试试这个:
$menu->addItem(数组("一"、"二"、"三");