【发布时间】:2011-04-01 01:19:36
【问题描述】:
我在 Apache 2.2.14 和 MySQL 5.1.48-community 上使用 CodeIgniter 2.0 和 PHP5.3.2。我创建了一个小型测试控制器来隔离另一个问题,并发现我的问题似乎是由公共变量可访问性引起的。调用 test1 或 test2 将导致错误,因为它们无法看到在其他函数中设置的数组元素的值。有谁知道为什么这不起作用?如果是这样,解决方案是什么,因为我需要能够访问类范围的变量。
谢谢。
<?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: */
【问题讨论】:
-
这些调用方式和顺序以及输出是什么?
-
错误信息的确切措辞是什么?
-
看这段代码无法判断...需要CI_Controller,以及错误信息的详细信息。
-
报错信息是:[code]A PHP Error was heard 严重性:Notice Message: Undefined index: test1 Filename: controllers/test.php Line Number: 26 [/code]
-
我猜第 26 行在
test1()中?您是否总是在拨打test1()之前先拨打index()?因为除非你这样做,否则索引 是 未定义。
标签: php oop codeigniter