简体   繁体   English

PHP全局变量范围?

[英]Php global variable range?

update: 更新:

the code i put below will be invoked by a form on other webpage. 我在下面放置的代码将由其他网页上的表单调用。 so that's why I didn't made a instance of a obj. 这就是为什么我没有创建obj的实例。

More detail code: 详细代码:

$serverloc='serverURL';

class Aclass{

           function push(){
             global $serverloc;
             echo $serverloc;
             funNotinClass();
           } 

           function otherFunction(){

              echo $serverloc;

           }
}

funNotinClass(){

      echo $serverloc;
}

There is a Class contains 2 functiona "push()" and "otherFunction()" and there is independent function "funNotinClass()" and push() calls it. 有一个包含两个函数的类“ push()”和“ otherFunction()”,还有一个独立的函数“ funNotinClass()”,push()调用了它。 The class is for a web form in other page. 该类用于其他页面的Web表单。 When user click submit the form call the class and use the push() function. 当用户单击提交表单时,调用该类并使用push()函数。 A weird thing I found is that the global var $serverloc is invisible to push() and funNotinClass()(they don't print out any thing), but otherFuction() which is a function just like puch() inside of the Aclass can just use the $serverloc(I dont even add global in front of it). 我发现一个奇怪的事情是,全局var $ serverloc对push()和funNotinClass()(它们什么都不会打印出来)不可见,但是otherFuction()的功能就像Aclass内部的puch()一样。可以只使用$ serverloc(我什至不在其前面添加global)。 How strange....anyone know what is the reason caused this? 多么奇怪....谁知道是什么原因造成的?

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++

I read many information about the scope of a global var in php. 我在php中阅读了许多有关全局var范围的信息。 they all say a global var is defined outside of function or class and you can use it by using global this key word. 他们都说全局变量是在函数或类之外定义的,您可以通过全局使用此关键字来使用它。

So this is my code 这是我的代码

$serverloc='serverURL';

class Aclass{

function Afunction(){
 global $serverloc;
 echo $serverloc;

} 
}

but when I run this class it didn't print anything out. 但是当我运行此类时,它没有输出任何内容。 Is that because I did something wrong or global var just doesn't work this way. 是因为我做错了什么,还是全局var不能这样工作。 Since all the example I read before are just access a global var in functions directly not a function in a class 由于我之前阅读的所有示例都只是直接访问函数中的全局变量而不是类中的函数

As per DaveRandom's comment - you haven't actually made an instance of an Aclass object. 根据DaveRandom的评论-您实际上尚未创建Aclass对象的实例。

This works and displays data as expected: 这可以正常工作并按预期显示数据:

<?php 
    $serverloc='serverURL';

    class Aclass{
        global $serverloc;      
        function Afunction()
        {
            echo  $serverloc;
        } 
    }

    $me = new Aclass();

    $me->Afunction(); // output: serverURL

?>

Edit: DaveRandom seems to post asnwers as comments. 编辑:DaveRandom似乎张贴asnwers作为注释。 Go Vote up some of his other answers, the rep belongs to him not me. 投票给他其他一些答案,代表属于他而不是我。 I am his ghostwriter tonight. 我今晚是他的代笔作家。

If it is class globals you are after you could do like 如果是全球学生,那么您会喜欢

class myClass
{    
  private $globVar = "myvariable";

  function myClass()
  {
    return $this->globVar;
  }
}

but when I run this class it didn't print anything out 但是当我运行此类时,它没有输出任何内容

You don't run classes , in the sense of executing them. 执行它们的意义上,您不运行 A class is a just a data structure that holds data and functions related (called methods). 类只是一个数据结构 ,其中包含相关的数据和功能(称为方法)。

As most traditional data structures, you create instances of them (called objects), and then you execute actions on them. 与大多数传统数据结构一样,您可以创建它们的实例(称为对象),然后对它们执行操作。 One way to execute actions on objects (instances of classes), is to pass a message for it to do something: that is calling a method. 对对象(类的实例)执行操作的一种方法是传递一条消息以使其执行某项操作:即调用方法。

So, in your case you could do: 因此,您可以执行以下操作:

$obj = new Aclass(); // create an object, instance of Aclass
$obj->Afunction();   // ask it to perform an action (call a method)

Having said that, sometimes you want to create a class only for grouping related functions, that never actually really share data within an object. 话虽这么说,有时您只想创建一个类来对相关功能进行分组,但实际上并没有真正在对象内共享数据。 Often they may share data through a global variable (eg.: $_SERVER , $_GET , etc). 通常,他们可以通过全局变量(例如: $_SERVER$_GET等)共享数据。 That may be the case of your design right there. 那里的设计就是这种情况。

Such classes can have its methods executed without never instantiating them, like this: 这样的类可以在不实例化的情况下执行其方法,如下所示:

Aclass::Afunction();

While relying on global variables is usually an indicator of quick'n dirty design, there are cases in which it really is the best trade-off. 尽管依赖全局变量通常是快速完成设计的指标,但在某些情况下,这确实是最好的折衷方案。 I'd say that a $serverlocation or $baseurl may very well be one of these cases. 我想说$serverlocation$baseurl可能就是这些情况之一。 :) :)

See more: 查看更多:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM