我希望使用该方法的各种键获得数组格式的值。
类文件
<?php
class Hooks
{
private $version = '1.4';
public $hook = array();
public function __construct(){ }
public function get_item_hook()
{
//bla bla bla
$this->hook['foo'] = 'resulting data for foo';
$this->hook['foo1'] = 'resulting data for foo1';
$this->hook['foo2'] = 'resulting data for foo2';
return $this->hook;
}
public function get_item2_hook()
{
//bla bla bla
$this->hook['another'] = 'resulting data for another';
$this->hook['another1'] = 'resulting data for another1';
$this->hook['another2'] = 'resulting data for another2';
return $this->hook;
}
}
?>
Code FIle
<?php
// this is in another file
include ('path to above class file');
$hook = new Hooks;
$hook->get_item_hook();
//now how can I get value of $this->hook array()???
$hook->get_item2_hook();
//now how can I get value of $this->hook array()???
?>
答案 0 :(得分:3)
调用方法时,您没有捕获返回值。
尝试
$myArray = $hook->get_item_hook();
// ^^ here we store the return value
print_r($myArray);
echo $myArray['foo']; // resulting data for foo
另外,正如bountyh指出的那样,您错过了方法中的function
关键字:
public function get_item_hook()
{
...
}
如果您打开PHP错误或检查错误日志,您应该会看到此错误:
解析错误:语法错误,意外T_STRING,期待T_VARIABLE ......
要打开错误:
error_reporting(E_ALL);
ini_set('display_errors', '1');
答案 1 :(得分:2)
只需将返回值分配给变量:
$item = $hook->get_item_hook();
print_r($item);
$item2 = $hook->get_item2_hook();
print_r($item2);
答案 2 :(得分:1)
您尚未将类文件中的&#34;函数定义为函数&#34;
例如是public function get_item2_hook()not public get_item2_hook()
答案 3 :(得分:0)
将数组分配给变量?
$array = $hook->get_item_hook();
print_r($array);