Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 全局变量不工作_Php_Global - Fatal编程技术网

Php 全局变量不工作

Php 全局变量不工作,php,global,Php,Global,我正在尝试用PHP创建自己的简单框架。一切正常,但我遇到了一个问题 我将所有内容重定向回index.php,然后从那里开始加载类和函数。我将url拆分为多个段,效果很好。。直到我想在类中使用段 我有这样一个主控制器类: class SimpleController { public function __construct() { global $uri; print_r($uri); } } 它可以很好地输出$uri变量,但是

我正在尝试用PHP创建自己的简单框架。一切正常,但我遇到了一个问题

我将所有内容重定向回index.php,然后从那里开始加载类和函数。我将url拆分为多个段,效果很好。。直到我想在类中使用段

我有这样一个主控制器类:

class SimpleController {

    public function __construct()
    {
        global $uri;
        print_r($uri);
    }
  }  
它可以很好地输出$uri变量,但是当我为我的主页创建一个新控制器时,我会这样做:

class home extends SimpleController{

private $template = 'home'; // Define template for this page

public function __construct()
{
    parent::__construct();
}

public function index()
{        


   print_r($uri);
  $view = new View($this->template); // Load the template

}}
现在它给了我一个错误,未定义的变量。既然我在父构造函数中使其成为全局的,这怎么可能呢

谢谢你的帮助

那是个糟糕的设计。 你不应该依赖全球国家。相反,将$uri传递到您家的构造器中。

这是一个糟糕的设计。 你不应该依赖全球国家。而是将$uri传递到您家的构造函数中。

不要在PHP中使用“全局”。。 只需在控制器中使用公共变量

新代码:

abstract class SimpleController {
    public $uri;

    public function __construct($uri)
    {
        $this->uri = $uri;
    }
}

class home extends SimpleController{
    private $template = 'home'; // Define template for this page

    public function index()
    {
        $this->uri; //This is the URI
        $view = new View($this->template); // Load the template
    }
}
要创建控制器,只需使用:

$controller = new home();
$controller->uri = "URI";
$controller->index();
编辑:从主页删除构造函数,当您想使用它时,还可以传递$uri。

不要在PHP中使用“全局”。。 只需在控制器中使用公共变量

新代码:

abstract class SimpleController {
    public $uri;

    public function __construct($uri)
    {
        $this->uri = $uri;
    }
}

class home extends SimpleController{
    private $template = 'home'; // Define template for this page

    public function index()
    {
        $this->uri; //This is the URI
        $view = new View($this->template); // Load the template
    }
}
要创建控制器,只需使用:

$controller = new home();
$controller->uri = "URI";
$controller->index();
编辑:从主目录中删除了构造函数,当您想使用它时,还可以传递$uri

既然我在父构造函数中使其成为全局的,这怎么可能呢

它是一个新的作用域,因此如果要访问它,必须再次将其标记为全局(
global$uri;

但这是一个糟糕的设计,使用成员或类变量

既然我在父构造函数中使其成为全局的,这怎么可能呢

它是一个新的作用域,因此如果要访问它,必须再次将其标记为全局(
global$uri;


但这是一个糟糕的设计,使用成员或类变量。

正如@yes123所说,您的设计非常糟糕,您应该避免使用全局变量。否则,您需要在每个函数中使用
global
。如果要使用全局变量,请将代码更改为:

public function index()
{
  global $uri;       
  print_r($uri);
  $view = new View($this->template); // Load the template

}

正如@yes123所说,您的设计非常糟糕,您应该避免使用全局变量。否则,您需要在每个函数中使用
global
。如果要使用全局变量,请将代码更改为:

public function index()
{
  global $uri;       
  print_r($uri);
  $view = new View($this->template); // Load the template

}

您在哪里定义了
$uri
?它现在可以工作了,但是在我的index.php中,我包含了一个名为routing.php的文件,在这个文件中,我将url拆分为段,并将它们放入$uri数组中。我现在已经将这个$uri传递到父控制器类的构造函数中,它像一个charm一样工作。您在哪里定义了
$uri
?它现在可以工作了,但是在我的index.php中,我包含了一个名为routing.php的文件,在那里我将url拆分为段,并将它们放入$uri数组中。我现在已经将这个$uri传递到父控制器类的构造函数中,它的工作方式就像charmI一样,可以进一步避免公共成员变量。改用getter和setter。我真的很想成为一名优秀的程序员,你能解释一下为什么我应该避免使用公共变量吗?谢谢我会更进一步,避免使用公共成员变量。改用getter和setter。我真的很想成为一名优秀的程序员,你能解释一下为什么我应该避免使用公共变量吗?谢谢