Global Variables vs. Local Variables

后端 未结 6 988
忘了有多久
忘了有多久 2021-01-12 21:36

Okay, I\'m not sure I\'m understanding this concept properly (I\'m learning PHP). If I understand correctly: Global variables can be referenced anywhere in the same document

6条回答
  •  心在旅途
    2021-01-12 22:15

    If you use only global vars, you'll need to give a new name to every new counter, haha.

    The visibility of vars is needed for encapsulation: variable can be affected only by object that you want to have access to it. For example:

    class Person{
      private $_money;
      public function rob(){
        return $this->_money;
      }
    }
    

    Only object of class Person can operate his money, so, if you want them, you have to get mask and gun.

提交回复
热议问题