为什么变量可以是静态的但不能是全局的

6 php oop

我想知道为什么我不能像global在中那样定义变量class.这样的事情:

class MyClass {
  global $var1 = 'anything'; // syntax error
  static $var2 = 'anything'; // but it is ok
}
Run Code Online (Sandbox Code Playgroud)

我知道static如果我想在没有创建类的实例的情况下使用它,我应该使用它.对 ?只有那个,没有别的东西?是global同样的工作?

事实上,我无法理解的概念,globalPHP和也,我不能和它相比static.我知道它们都保留了值,但我认为不同的是在生命周期 (存储持续时间)范围之间.所以globalstatic什么时候应该使用,什么时候应该使用?

Mar*_* AO 1

global您在房产中需要什么声明class?我们在这里不处理简单的事情functions$this->var1将从类内部的$var1任何method位置或类外部的实例化对象(其中变量为 )获取您的信息public但让我们彻底地...

诠释“全球”

Global 对类属性没有意义;尽管您可以将它与类方法内的变量一起使用,就像使用常规函数一样。(但是有更优雅的方法可以到达那里;最好避免在全局范围内出现潜在危险的混乱负载。)让我们首先定义一个变量:

$globby = 123; // Globby is a friend living outside a function.
Run Code Online (Sandbox Code Playgroud)

不使用global声明的函数既不能从其作用域之外访问变量,也不能更改该变量的值。故事是这样的:

function foo() {
    echo $globby; // Here we have no clue who globby is?
    $globby = 321; // Here we define globby, but then he's totally forgotten. :(
}
foo(); // => 'Notice: Undefined variable: globby'

echo $globby; // => '123' ... still the same old globby.
Run Code Online (Sandbox Code Playgroud)

但是,将变量声明为 as 的函数global既可以在函数作用域之外访问它也可以修改它。此外,在函数内部新定义为全局的变量可以在函数外部访问。

function foot() {
    global $globby; // Hey globby, won't you come on in! *Globby UFO Lands*
    echo $globby; // - Who are you? - I'm globby that says 123.
    $globby = 321; // - But I'm gonna tell you to say 321 everywhere.
}

foot(); // => '123' ... this here is the good old globby from the start.
echo $globby; // => '321' ... see, globby can change outside the function scope!
Run Code Online (Sandbox Code Playgroud)

说明课堂上的“静态”

请注意,static属性的static工作方式与函数变量的工作方式不同。手册:“静态变量仅存在于局部函数作用域中,但当程序执行离开此作用域时,它不会丢失其值。” 再说一遍:“将类属性或方法声明为静态可以使它们无需实例化类即可访问。” OOP 静态关键字;和使用静态变量)在任何情况下,类属性都会在对象的生命周期内保留其值。

现在,来说明静态和非静态方法(又名“类函数”)和属性(又名“类变量”)的用法(和不使用)。我们来上个小课:

class foo {

    static $one = 1; // This is a static variable aka. class property.
    var $two = 2; // But this is non-static.

    static function say_one() { // This is a static method.
        echo self::$one; // Uses self:: to statically access the property.
    }

    function say_two() { // This is a non-static method.
        echo $this->two; // Uses $this-> to dynamically access the property.
    }

}
Run Code Online (Sandbox Code Playgroud)

然后让我们看看什么有效,什么无效。非工作选项被注释掉。

/* Static Variables and Methods */

echo foo::$one; // => '1'
echo foo::say_one(); // => '1'

// echo foo::$two; 
// => "Fatal error: Access to undeclared static property: foo::$two"

// echo foo::say_two(); 
// => "Strict: Non-static method foo::say_two() should not be called statically. 
// & Fatal error: Using $this when not in object context."


/* Non-Static Variables and Methods */

$f = new foo(); // This here is a real effin' instantiated dynamite object. *BOOM*

echo $f->two; // => '2' 
echo $f->say_two(); // => '2'

// echo $f->one; 
// => "Strict: Accessing static property foo::$one as non static. 
// & Notice: Undefined property: foo::$one."

echo $f->say_one(); // => '1'
Run Code Online (Sandbox Code Playgroud)

希望能澄清。请注意,您可以通过实例化对象访问静态方法,并使用它来访问静态变量;但是您不能在没有警告的情况下直接将静态变量作为非静态访问。

让我添加一个关于良好实践的注释。如果您发现需要global在函数内重复声明变量,或者将配置参数等作为函数参数传递,则表明您可能应该将代码分解到类中,将这些全局变量作为其属性进行访问。OOP 使用起来更加干净。你会更快乐地编码。$OOP->nirvana();