你有幾個語法錯誤提示你應該閱讀PHP manual有關基礎知識。
你的代碼(格式化):
<?php
class remoteControl {
public $operate = array("ON", "OFF", "UP","DOWN"); // 1)
public function pressButton($operate("0")) { // 2), 3), 4)
echo "You have pressed ". $this->operate; // 5)
}
}
$control_01 = new remoteControl();
echo $control_01-> pressButton();
?>
1)如果只使用類方法中使用
2)陣列,你應該讓這個私有變量:$操作[0] - read more
3)不要使用字符串作爲索引(「0」) - 它會工作,但它的不必要的類型鑄造
4)最後,此行應該是這樣的:
public function pressButton($operate = 0) {
這意味着,如果你沒有明確提供一個參數將有0值 - 因爲4 read more about function arguments
5) )它應該是:
echo "You have pressed ". $this->operate[$operate];
編輯:所有代碼:
<?php
class remoteControl {
private $operate = array("ON", "OFF", "UP", "DOWN");
public function pressButton($operate = 0) {
echo "You have pressed ". $this->operate[$operate];
}
}
$control_01 = new remoteControl();
echo $control_01->pressButton();
?>