注意:数组到字符串的转换 - 为什么?
您好,我尝试执行以下 PHP 代码,但是收到错误。我将引用传递到核心类中,我想将其分配给类范围内的变量。
注意:数组到字符串的转换
提前致谢。
$core = new core($config);
$core->execute();
class core
{
private $config;
public function __construct(&$config)
{
$this->$config = $config;
}
public function execute()
{
$this->set_path();
}
private function set_path()
{
return true;
}
}
Hi im trying to execute the following PHP code, however im receiving an error. Im passing a reference into the core class, which i want to assign to a variable within the classes scope..
Notice: Array to string conversion
Thanks in advance..
$core = new core($config);
$core->execute();
class core
{
private $config;
public function __construct(&$config)
{
$this->$config = $config;
}
public function execute()
{
$this->set_path();
}
private function set_path()
{
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,首先......
应该删除
$config
中的第二个$
,否则它会尝试使用中的字符串给出的名称访问变量$config
. (例如,如果 $config 将"test"
作为值,您将访问类中的"test"
变量:$this-> test
)传入时,
$config
是什么? (字符串、数组、对象等?)Well, first off....
The second
$
in$config
should be removed since otherwise it's trying to access the variable with the name given by the string within$config
. (e.g., if $config held"test"
as a value, you'd be accessing the"test"
variable within your class:$this->test
)What is
$config
when it is passed in, anyway? (String, array, object, etc?)私有 $config = 数组();
private $config = array();
$this->config = $config;
$this->config = $config;
这在 php 5.2 中没有错误。
您使用什么版本的 php?
This works without errors in php 5.2.
What version of php are you using?