PHP global in functions

后端 未结 7 1149
梦谈多话
梦谈多话 2020-11-21 05:41

What is the utility of the global keyword?

Are there any reasons to prefer one method to another?

  • Security?
  • Performance?
  • Anything els
7条回答
  •  难免孤独
    2020-11-21 06:23

    It makes no sense to make a concat function using the global keyword.

    It's used to access global variables such as a database object.

    Example:

    function getCustomer($id) {
      global $db;
      $row = $db->fetchRow('SELECT * FROM customer WHERE id = '.$db->quote($id));
      return $row;
    }
    

    It can be used as a variation on the Singleton pattern

提交回复
热议问题