# [patch] Dynamic access of static members/methods, and constants

- Thread: https://externals.io/message/28276
- Original archive: https://news-web.php.net/php.internals/28276
- Started by Etienne Kneuss on 2007-03-06T15:06:55+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #28276 — Etienne Kneuss — 2007-03-06T15:06:55+00:00

URL: https://externals.io/message/28276#28276

Hi,

It may have already been proposed, but I give it a shot:
I believe it would be nice to be able to dynamically reference static 
members and constants.

Currently, there is no way to do that and the only way would be to 
create a static method and use call_user_func(array($classname, 'getThat'));

In other words:

class A {
    public static $foo = 'bar';
    const gee = 'zzz';

    public static function getFoo() { return self::$foo; }
}

$classname = 'A';
echo $classname::$foo; // bar
echo $classname::gee; // zzz

// instead of:
echo call_user_func(array($classname, 'getFoo'));

Please take a look at the patch I made for it.

For consistency purposes, I also implemented $classname::myMethod(); 
even if it's not required. FWIW, I've no strong feeling about it.

Regards,

-- 
Etienne Kneuss
http://www.colder.ch
colder@php.net

Men never do evil so completely and cheerfully as 
when they do it from a religious conviction.
-- Pascal

---

## #28277 — Marcus Boerger — 2007-03-06T22:20:05+00:00

URL: https://externals.io/message/28276#28277 · In reply to: #28276

Hello Etienne,

  besides a tiny bit of whitespace issues the patch looks fine. Actually we
use tabs at the beginning of lines here (prior and after |'s). Once you got
more people looking/commenting on the patch we mde decide whetehr to apply
it.

best regards
marcus

Tuesday, March 6, 2007, 4:06:55 PM, you wrote:

> Hi,

> It may have already been proposed, but I give it a shot:
> I believe it would be nice to be able to dynamically reference static 
> members and constants.

> Currently, there is no way to do that and the only way would be to 
> create a static method and use call_user_func(array($classname, 'getThat'));

> In other words:

> class A {
>     public static $foo = 'bar';
>     const gee = 'zzz';

>     public static function getFoo() { return self::$foo; }
> }

> $classname = 'A';
> echo $classname::$foo; // bar
> echo $classname::gee; // zzz

> // instead of:
> echo call_user_func(array($classname, 'getFoo'));

> Please take a look at the patch I made for it.

> For consistency purposes, I also implemented $classname::myMethod(); 
> even if it's not required. FWIW, I've no strong feeling about it.

> Regards,




Best regards,
 Marcus
