php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62860 Moar magic methods! __constructStatic(), __getStatic(), __setStatic(), __get
Submitted: 2012-08-18 20:03 UTC Modified: 2021-08-10 16:37 UTC
Votes:24
Avg. Score:3.5 ± 1.8
Reproduced:13 of 20 (65.0%)
Same Version:6 (46.2%)
Same OS:6 (46.2%)
From: michaelduff2 at yahoo dot com Assigned:
Status: Suspended Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: michaelduff2 at yahoo dot com
New email:
PHP Version: OS:

 

 [2012-08-18 20:03 UTC] michaelduff2 at yahoo dot com
Description:
------------
__constructStatic() - executed automatically on class definition

__getStatic() - executed when ClassName::$inaccessible_property is fetched

__setStatic() - executed when ClassName::$inaccessible_property is modified


The particular use case I have for this is self-loading configuration registry singletons:

<?php7

namespace Company;

class Config
{
    protected static $settings;

    function __constructStatic($property)
    {
        static::$settings = parse_ini_file('config.ini');
    }

    function __getStatic($property)
    {
        return static::$settings[$property];
    }
}

echo Config::$DB_USER;

?>

Currently, I accomplish this with __callStatic() which needs the extra open-close parenthesis.  Ideally, we could get rid of the '$' too, but that would need some __getConst() magic, which is just madness.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-05 15:06 UTC] matthew dot bonner at gmail dot com
The additional magic methods with static support have been requested over and over for well over 5 years now. Is PHP a dying language because the developers don't have the time to provide essential functionality?

There are so many hacks going about now to try and provide essential functionality and there is so much messy code all over the place to support what should be part of PHP 5 as standard.

Sorry for my raving and ranting but the reputation of PHP is being severely damaged by not providing the essentials.

.NET has static accessor and mutator support, Java does too. All the big languages do apart from PHP.

class PLEASE_SUPPORT_AT_A_MINIMUM {
  public static function __construct () {}
  public static function __set ($propertyName, $propertyValue) {}
  public static function __get ($propertyName) {}

  public function setConstDynamically () {
    const Foo = 'foo';
    echo self::Foo;
  }
}

Otherwise I will fork PHP and provide this essential functionality myself which will ultimately result in the death of the original PHP.
 [2012-09-05 15:10 UTC] matthew dot bonner at gmail dot com
I would like to add that being magic methods, it would be nice if they could do a little more magic, ie:

class Classy {
  public static $foo = 'foo';
  public $bar = 'bar';

  public static function __get ($propertyName) {
    return $this->$propertyName;
  }

  public function __get ($propertyName) {
    return self::$propertyName;
  }
}

echo Classy::$foo; // outputs "foo"
$classy = new Classy;
echo $classy->bar; // outputs "bar"
 [2012-09-05 15:12 UTC] matthew dot bonner at gmail dot com
Sorry in my last example there was a typo, and what I meant was:
I would like to add that being magic methods, it would be nice if they could do a little more magic, ie:

class Classy {
  public static $foo = 'foo';
  public $bar = 'bar';

  public static function __get ($propertyName) {
    return self::$propertyName;
  }

  public function __get ($propertyName) {
    return $this->$propertyName;
  }
}

echo Classy::$foo; // outputs "foo"
$classy = new Classy;
echo $classy->bar; // outputs "bar"
 [2012-09-12 02:41 UTC] reeze@php.net
Duplicate to https://bugs.php.net/bug.php?id=45002 closed.
 [2012-09-12 02:41 UTC] reeze@php.net
-Status: Open +Status: Duplicate
 [2012-09-12 02:52 UTC] laruence@php.net
-Status: Duplicate +Status: Re-Opened
 [2012-09-12 02:52 UTC] laruence@php.net
they are not dup...
 [2012-09-12 03:35 UTC] laruence@php.net
<GoogleGuy> The fact that his code defies the point of using magic getters and 
setters should have been a hint.
<GoogleGuy> __get and __set are meant for inaccessible variables.
 [2012-09-12 03:37 UTC] laruence@php.net
-Status: Re-Opened +Status: Open
 [2014-05-08 18:04 UTC] levim@php.net
This is at least related to bug 45002.
 [2015-04-24 05:34 UTC] michaelduff2 at yahoo dot com
RE: levim at php dot net

It is loosely related, however as GoogleGuy points out, Request #45002 mistakenly refers to __get and __set which are for object properties, not class properties; whereas this one more explicitly refers to accessors for static properties.

This request also includes a new heuristic: __constructStatic() which that request does not.  Should I break that part of this request out?

Finally, have the patches submitted by (thekid at php dot net) or (allesbesser at gmail dot com) from Request #45002 been vetted and incorporated yet?
 [2021-08-10 16:37 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-08-10 16:37 UTC] cmb@php.net
This feature request clearly needs someone to pursue the RFC
process[1].  For the time being I'm suspending this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 02:01:28 2024 UTC