在以下代码中,我想在echo
之外green
public function
。
Public function lol(){
$green ="green";
}
例如,我想在以下代码中回显$green
。
public function green(){
echo"this is $green";
}
答案 0 :(得分:2)
尝试以下代码:
function __construct() {
parent::__construct();
$green ="green";
}
public function green(){
echo"this is $green";
}
将$ green变量放在构造函数中。
答案 1 :(得分:0)
您将$green
作为参数传递并回显函数返回值:
function green($green) {
return "This is ". $green;
}
echo green('green'); //Results in: This is green
echo green('yellow'); //Results in: This is yellow
答案 2 :(得分:0)
$green;
class s {
public function lol() {
$GLOBALS['green'] = "green";
}
}
$instance = new s();
$instance->lol();
echo $green;
答案 3 :(得分:0)
将global variables与$GLOBALS
:
function lol(){
$GLOBALS['green'] ="green";
}
lol();
echo $green;