php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52225 FR: __getStatic() and __setStatic() for static classes
Submitted: 2010-07-01 19:51 UTC Modified: 2014-05-08 17:58 UTC
Votes:12
Avg. Score:4.6 ± 0.6
Reproduced:9 of 9 (100.0%)
Same Version:4 (44.4%)
Same OS:5 (55.6%)
From: lucato at gmail dot com Assigned:
Status: Duplicate Package: Scripting Engine problem
PHP Version: master OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
49 - 32 = ?
Subscribe to this entry?

 
 [2010-07-01 19:51 UTC] lucato at gmail dot com
Description:
------------
Would it be possible to have magic setter/getter functions available on static 
context ?

The documentation says "Property overloading only works in object context. These 
magic methods will not be triggered in static context."

I would find it useful on a static context where I have a class A extending 
another class B, to access



Test script:
---------------
Example:

Class A {

   static $data = array('bar' => 99, 'foo' => 101);
   
   function __get($name) {

       if (isset(self::$data[$name])) 
           return self::$data[$name];

       return null;
   }
}

Class B extends A {
   
}


echo B::$foo;

Expected result:
----------------
echo B::$foo; 

In the example should return 101.

Actual result:
--------------
In the example it triggers a fatal error.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-08 14:45 UTC] henrik at bearwoods dot dk
In 5.3 it works if you change self:: to static:: like this 

Test:
<?php
class A {

   static $data = array('bar' => 99, 'foo' => 101);
   
   function __get($name) {

       if (isset(static::$data[$name])) 
           return static::$data[$name];

       return null;
   }
}

class B extends A {
   
}


$object = new B();

print $object->foo;

Result
------
101
 [2010-10-03 19:00 UTC] + at ni-po dot com
@henrik: This is about static property overloading, not about late static binding ;)
 [2013-05-03 19:29 UTC] mkutschker-php at yahoo dot com
Now that we have __callStatic() a pair a setter/getter pair __getStatic()/__setStatic() would be the next logical step to take.
 [2013-07-31 01:37 UTC] yohgaki@php.net
-Summary: __get() and __set() for static classes +Summary: FR: __getStatic() and __setStatic() for static classes -Package: Class/Object related +Package: Scripting Engine problem -PHP Version: 5.3.2 +PHP Version: master
 [2014-05-08 17:58 UTC] levim@php.net
-Status: Open +Status: Duplicate
 [2014-05-08 17:58 UTC] levim@php.net
This is a duplicate of bug 45002.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 03:01:27 2024 UTC