如何回应功能之外的东西?

时间:2015-04-01 10:57:48

标签: php

在以下代码中,我想在echo之外green public function

Public function lol(){
$green ="green";
}

例如,我想在以下代码中回显$green

public function green(){
echo"this is $green";
}

4 个答案:

答案 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;