在我的PHP脚本中,我收到错误variable $command_in_hostfile undefined
。
我班上有两个功能。
在第一个函数中,我返回一个数组($command_in_hostfile
)以在另一个函数中使用它。
但是,我收到错误“变量未定义”,我不明白为什么。任何人都可以帮我纠正这个错误吗?
代码:
include_once('service_function.php');
if(isset($_POST['edit_cfg_file']))
{
$Host = new Host;
$Host->write_hostfile();
}
public $command_in_hostfile = array();
class Host
{
public $command_in_hostfile = array();
function read_services()
{
...
$command_in_hostfile[0] = array_merge($command_in_hostfile[0]);
$command_in_hostfile[1] = array_merge($command_in_hostfile[1]);
return($command_in_hostfile);
}
function write_hostfile()
{
foreach($command_in_hostfile[0] as $key=>$value)
{
$checkcommand[$key] = "check_command ".$value."!".$value2_converted."\n";
}
}
}
?>
答案 0 :(得分:0)
尝试使用$ this->
$command_in_hostfile = array();
class Host
{
public $command_in_hostfile = array();
function read_services()
{
...
$this->command_in_hostfile[0] = array_merge($command_in_hostfile[0]);
$this->command_in_hostfile[1] = array_merge($command_in_hostfile[1]);
return($this->command_in_hostfile);
}
function write_hostfile()
{
foreach($this->command_in_hostfile as $key=>$value)
{
$checkcommand[$key] = "check_command ".$value."!".$value2_converted."\n";
}
}
}