【问题标题】:Return global value from function to class从函数返回全局值到类
【发布时间】:2014-04-12 11:35:36
【问题描述】:

如何将全局 $here $that $this 返回到我的 new2()

new1()
{
 global $that;
 global $this;//every global will return
 global $here;
 $here="true";
 return $here;
}   
new2($here)
{
       //i dont have $here
}
class Main {
    public function parametr(){ 
        new1();
        new2($here)

    }

【问题讨论】:

  • 在我用您的代码解释所有内容时查看我的答案。

标签: php class return


【解决方案1】:

首先,你不能有函数new(),所以我用new_()代替了它。其次,如果要使用全局$here而不是本地,则需要在函数开头添加global $here。所以代码看起来像:

$here = '';
function new_() {
     global $here;
     $here = "true";
     return $here;
}   
function new2() {
    global $here;
    echo $here;
}
new_();
new2();

更新: 你也不能在 Main 类中传递 $here,因为你没有声明它是全局的 $here,所以它使用的是本地的 $here,它是空的。与new2()中的参数$here声明函数相同,如果要全局$here,则不能添加参数,因为它会在这里取本地。

【讨论】:

  • 为什么我不能在课堂上的公共函数中使用函数 new()
  • @user3397383 - 你不能有函数 new() 因为new 是 PHP 中的保留字:它在你实例化一个类时使用
  • 正如@MarkBaker 所说。如果你想知道 PHP 还保留了哪些单词,请查看 us3.php.net/manual/en/reserved.keywords.php
猜你喜欢
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
相关资源
最近更新 更多