我试图调用函数中正在创建的变量,如何访问此变量?
<?php
class youtube
{
public function details()
{
$test = "asdas";
}
}
$class = new youtube();
$s = $class->details()->test;
echo $s;
?>
答案 0 :(得分:2)
有几种方法可以做到,但我会做这样的事情。
<?php
class youtube
{
private $details = array(
'test' => null
);
public function details()
{
return $this->details;
}
}
$class = new youtube();
$s = $class->details()->test;
echo $s;
基本上我存储了一个包含各种属性的details属性,我只返回这个对象。
答案 1 :(得分:1)
正如@Feroz所建议的那样,你没有从details()
方法返回任何内容:
class youtube
{
public function details()
{
$test = "asdas";
return $test;
}
}
$class = new youtube();
$s = $class->details();
echo $s;
或
class youtube
{
public $test;
public function details()
{
$this->test = "asdas";
return $this;
}
}
$class = new youtube();
$s = $class->details()->test;
echo $s;