Why a variable can be static, but cannot be global in a class

I want to know why I cannot define a variable as global in a class . something like that:

 class MyClass { global $var1 = 'anything'; // syntax error static $var2 = 'anything'; // but it is ok } 

I know that I should use static if I want to use this without instantiating the class. right? Is it just that and nothing else? Is global the same job?

Actually, I can not understand the concept of global in php, and also can not compare it with static . I know that both of them retain values, but I think that the difference is between life expectancy (storage duration) or, possibly, the area . So when should global be used and when should static be used?

+6
source share
2 answers

Why do you need a global declaration for a class property? We are not dealing with the usual functions here. $this->var1 will get you $var1 from either method inside the class or from an object instance outside the class where the public variable. But let it be thorough ...

Illustration "Global"

Global does not matter for class properties; although you can use it with variables inside class methods, just like you can do with regular functions. (But there are more elegant ways to get there, it is better to avoid loading a potentially dangerous mess in a global area.) First, define a variable:

 $globby = 123; // Globby is a friend living outside a function. 

A function that does not use the global declaration cannot access the variable from outside its scope or change the value of the variable. The story is as follows:

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

However, a function that declares a variable as global can access it and modify it outside the scope. In addition, a variable defined as global inside a function is available outside of it.

 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! 

The designation "Static" in classes

Remember that static for a class property does not work as static for a function variable. Manual: "A static variable exists only in the area of ​​a local function, but it does not lose its value when the program execution leaves this area." And again: "Declaring class properties or methods as static makes them available without having to instantiate the class." ( OOP Static Keyword and Using Static Variables ) In any case, class properties retain their value for the lifetime of an object.

Now, to illustrate the use (and non-use) of static and non-static methods (the so-called "class functions") and properties ("class variables"). Let's make a small class:

 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. } } 

Then let's see what works and what doesn’t. Inactive parameters are commented out.

 /* 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' 

Hope this clears up. Note that you can access the static method through an instance of the object and use it to access the static variable; but you cannot directly refer to static variables as non-static without warnings.

Let me add a note about good practice. If you find that you need to repeatedly declare global variables inside your functions or pass configuration parameters, etc. As a function argument, this is a sign that you should probably factor your code into a class instead, by accessing these global variables as your properties, OOP is so much cleaner to work with. You will make the code happier. $OOP->nirvana(); .

+1
source

static and global are used for different purposes.

  • if you want your value not to change around your project or code, you will need a static one.

  • But if you want some variables to be outside the function and use them directly without passing them as a parameter, you need a global one. which retrieves the value of a variable that is globally accessible, and its value can be changed or changed.

     $suv = "uvre"; function global_access_tes() { //want to use suv inside this function global $suv; $suv = "changed"; } global_access_tes(); echo $suv; // output - changed 
+2
source

Source: https://habr.com/ru/post/989882/


All Articles