我從php.net網站裏面的代碼的類屬性:修改類功能
<?php
class foo {
public $foo;
public $bar;
public function foo() {
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
echo <<<EOT
I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
EOT;
?>
但我很困惑,什麼是每一個「富」代表什麼?你真的可以在不調用函數的情況下修改方法(函數)中的屬性(變量)嗎?
我寫了類似的代碼,但什麼都沒有發生
<?php
class page {
public $meta;
public $buttons;
public function show_meta() {
$this->meta = "I'm a website";
$this->buttons = array(
"home" => "index.php",
"about" => "about.php"
);
}
}
$index = new page();
echo $index->meta;
echo $index->buttons["home"];
?>
我是一個PHP的學習者,我需要你的幫助,請:)
你得到的代碼有什麼錯誤?你可以修改變量,因爲它們是公開的,它們是私人的,你不能這樣做。 – 2013-02-16 17:23:33
我想如果你在構造函數中聲明按鈕數組,你會得到你想要的結果。 'show_meta()'方法沒有被調用,因此你的數組沒有被初始化。您必須至少調用一次該方法來初始化數組。糾正我,如果我worng – harsh8888 2013-02-16 17:29:24