我在使用MySQL 5.1.48社區的Apache 2.2.14上使用CodeIgniter 2.0和PHP5.3.2。我創建了一個小型測試控制器來隔離另一個問題,並發現我的問題似乎是由公共變量訪問引起的。調用test1或test2將導致錯誤,因爲它們看不到其他函數中設置的數組元素的值。有沒有人有任何想法,爲什麼這不起作用?如果是這樣,我需要能夠訪問全班變量的解決方案是什麼。爲什麼我不能從類函數訪問PHP5類中的公共變量的值?
謝謝。
<?php
class Test extends CI_Controller
{
public $data;
function __construct()
{
parent::__construct();
$this->data = array();
}
function index()
{
$this->data['test1'] = 'This is a test of class public variable access.<br />';
echo 'Class index() called.<br />';
echo $this->data['test1'];
}
function test1()
{
$this->data['test2'] = 'This is a second test of the class public variable access.<br />';
echo 'Class test1 called.<br />';
echo $this->data['test1'];
echo $this->data['test2'];
}
function test2()
{
echo 'The data array contains these two entries:<br />';
echo $this->data['test1'];
echo $this->data['test2'];
}
}
/* End of file test.php*/
/* Location: */
這些調用的順序和順序是什麼以及輸出是什麼? – deceze 2011-04-01 01:22:52
錯誤信息的確切用詞是什麼? – Unsigned 2011-04-01 01:43:37
無法從查看此代碼中看出...需要CI_Controller以及錯誤消息的詳細信息。 – 2011-04-01 06:24:04