66 links
  • Ban's links
  • Home
  • Login
  • RSS Feed
  • ATOM Feed
  • Tag cloud
  • Picture wall
  • Daily
Links per page: 20 50 100
  • Why You Should Use Class Static Rather Than Globals in PHP
    • many forget global variables are fully global, not only to the current file;
    • declaration outside any other scope in the file doesn't necessarily mean the variable will be global, e.g. if the current file is included from a non-root scope. The only completely reliable way to declare a global is to use $GLOBALS;
    • fully global variables are generally a bad idea anyway.

    Instead, if you need a similar feature, look at class (or function) static variables.

    • they have the expected scope;
    • support access restriction (private, protected, public);
    • can be referenced from the using class with the better syntax self::$my_class_global.

    If you don't have a class… well, first check if your code couldn't get better organized by being in one. If not, either create a state class (abstract class MyState { static $foo = 0; }) and use it as a global, or know what you do and use a global, with all its attached baggage.

    March 22, 2014 at 18:03:42 GMT+1 * - permalink -
    QRCode
    - http://php.net/manual/en/language.variables.scope.php
    php dev web style trick
Links per page: 20 50 100
Shaarli - The personal, minimalist, super fast, database-free, bookmarking service by the Shaarli community - Help/documentation