2

Possible Duplicate:
Magic __get getter for static properties in PHP

Is it possible to catch a call to a static parameter like this:

class foo
{
    public static $foo = 1;
    //public static $bar = 2; 

    public function catch_bar_call()
    {
        print "Calling an undefined property";
    }
}


print foo::$foo //1
print foo::$bar //error

instead of getting an error, i want a method to be called. i know its possible trough __get() magic method, but you'll have to instantiate your class for this, not possible on static parameter.

Community
  • 1
  • 1
M K
  • 9,138
  • 7
  • 43
  • 44

1 Answers1

0

From PHP DOC

The $name argument is the name of the property being interacted with. The __set() method's $value argument specifies the value the $name'ed property should be set to.

Property overloading only works in object context. These magic methods will not be triggered in static context. Therefore these methods should not be declared static. As of PHP 5.3.0, a warning is issued if one of the magic overloading methods is declared static.

What i think you can do is

class Test {
    private static $foo = 1;
    // public static $bar = 2;
    public static function foo() {
        return self::$foo;
    }

    public static  function __callStatic($n,$arg) {
         print "\nCalling an undefined property $n";
    }
}
echo "<pre>";
print Test::foo(); // 1
print Test::bar(); // Calling an undefined property bar

Output

1
Calling an undefined property bar
Baba
  • 94,024
  • 28
  • 166
  • 217
  • That handles the problem wihile calling a static function, but not calling a static parameter/attribute. try Test::$bar (it's not set and throws an error. what i want is to set a kind of listener to catch all calls on attributes/parameters, and react if an attribute is not persistant) – M K Jan 07 '13 at 15:34