Why is the global $ object_name discouraged?

I have two classes, and currently I am referencing one another using this:

ClassB :: func ()
{
    global $ classAObject;
    echo $ classAObject-> whatever ();
}

However, I was told that use is globalnot recommended. This and why?

+3
source share
2 answers

There are many reasons not to use global variables. Here are just a few:

  • Region
    • On a large system, it can be easy to accidentally reassign global variables if you reuse a semi-common name
    • Variables on a global scale increase the memory capacity of your scripts. Not always important, but may be
    • - - ,
    • . .
    • . .

, , - , .

classB::func($obj) 
{
   echo $obj->whatever();
}

$obj = new classAObject;
classB::func($obj);
+7

, . :

ClassB::func($classAObject)
{
    echo $classAObject->whatever();
}
0

All Articles