Viewed   80 times

I was looking at the source for Drupal 7, and I found some things I hadn't seen before. I did some initial looking in the php manual, but it didn't explain these examples.

What does the keyword static do to a variable inside a function?

function module_load_all($bootstrap = FALSE) {
    static $has_run = FALSE

 Answers

2

It makes the function remember the value of the given variable ($has_run in your example) between multiple calls.

You could use this for different purposes, for example:

function doStuff() {
  static $cache = null;

  if ($cache === null) {
     $cache = '%heavy database stuff or something%';
  }

  // code using $cache
}

In this example, the if would only be executed once. Even if multiple calls to doStuff would occur.

Saturday, November 26, 2022
5

If your class is not meant to define some super-type, it should not be declared as abstract, I'd say.

In your case, I would rather go with a class :

  • That defines __construct and __clone as private methods
    • so the class cannot be instanciated from outside
  • And, this way, your class could create an instance of itself
    • See the Singleton design pattern, about that, btw


Now, why use a Singleton, and not only static methods ? I suppose that, at least a couple of reasons can be valid :

  • Using a singleton means using an instance of the class ; makes it easier to transform a non-singleton class to a singleton one : only have to make __construct and __clone private, and add some getInstance method.
  • Using a singleton also means you have access to everything you can use with a normal instance : $this, properties, ...
  • Oh, a third one (not sure about that, but might have its importance) : with PHP < 5.3, you have less possibilities with static methods/data :
    • __callStatic has only been introduced in PHP 5.3
    • There is no __getStatic, __setStatic, ...
    • Same for a couple of other Magic methods !
  • Late Static Binding has only been added with PHP 5.3 ; and not having it often makes it harder, when working with static methods/classes ; especially when using inheritance.


This being said, yes, some code like this :

abstract class MyClass {
    protected static $data;
    public static function setA($a) {
        self::$data['a'] = $a;
    }
    public static function getA() {
        return self::$data['a'];
    }
}

MyClass::setA(20);
var_dump(MyClass::getA());

Will work... But it doesn't feel quite natural... and this is a very simple example (see what I said earlier with Late Static Binding, and magic methods).

Sunday, September 4, 2022
 
2

Yes 7.1.1/6

A name declared in a namespace scope without a storage-class-specifier has external linkage unless it has internal linkage because of a previous declaration and provided it is not declared const.

See also the examples of 7.1.1/7

Thursday, August 4, 2022
 
2

This is non standard. I'd guess the same thing as you, and I'm not surprised of such extension in compilers having an embedded target.

Sunday, November 6, 2022
 
watis
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :