# $var::$static

- Thread: https://externals.io/message/29801
- Original archive: https://news-web.php.net/php.internals/29801
- Started by Bart de Boer on 2007-05-26T11:50:04+00:00
- Messages: 10
- 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

---

## #29801 — Bart de Boer — 2007-05-26T11:50:04+00:00

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

Hi all,

I'd like to be able to do the following:


<?php

class Base {

	public static $var = 'hello';

	public function someFunc() {
		echo self::$var; // Currently maps to Base::$var
		echo $this::$var; // Should map to Child::$var
	}

}

class Child extends Base {

	public static $var = 'hello';

}

$class = 'Child';

$obj = new $class(); // This works.

echo $class::$var; // This doesn't. Should map to Child::$var


?>


...in other words: I'd like to be able to access static class variables 
from inside an instance of the Base and/or Child classes. I'd also like 
to be able to access them dynamically. ($className::$variable)

The only way to do this at the moment (to my knowledge at least) is to 
create functions in the Child class that returns its static variables. 
The downside of this is that those functions most likely will be very 
common (in my case they are) and should therefore belong in the base 
class. Hence: $this::$variable

At the moment there is no way to access static variables from outside of 
the class dynamically. As a workaround for this I'm currently creating a 
temporary instance (new $type()) to access them dynamically with a 
__get() function in all the derived child classes.

There are ways to do it with class constants and the constant functions. 
But it's not very elegant and class constants can't hold arrays and/or 
objects.

I have no idea what the implications would be. Just thought it would be 
a nice addition to the language. :) Hope I didn't overlook some existing 
PHP feature that already allows me to do this. :|


Cheers,

Bart de Boer

---

## #29802 — Marco Kaiser — 2007-05-26T11:57:31+00:00

URL: https://externals.io/message/29801#29802 · In reply to: #29801

Hi,

this doesnt work because static vars are bound to his class and are not
inherited by a child class.
maybe this would be work with php6 or a other 5.x version.
(Same behavior like the singleton pattern getInstance() abstract class
stuff)

-- Marco
> Hi all,
>
> I'd like to be able to do the following:
>
>
> <?php
>
> class Base {
>
>     public static $var = 'hello';
>
>     public function someFunc() {
>         echo self::$var; // Currently maps to Base::$var
>         echo $this::$var; // Should map to Child::$var
>     }
>
> }
>
> class Child extends Base {
>
>     public static $var = 'hello';
>
> }
>
> $class = 'Child';
>
> $obj = new $class(); // This works.
>
> echo $class::$var; // This doesn't. Should map to Child::$var
>
>
> ?>
>
>
> ...in other words: I'd like to be able to access static class
> variables from inside an instance of the Base and/or Child classes.
> I'd also like to be able to access them dynamically.
> ($className::$variable)
>
> The only way to do this at the moment (to my knowledge at least) is to
> create functions in the Child class that returns its static variables.
> The downside of this is that those functions most likely will be very
> common (in my case they are) and should therefore belong in the base
> class. Hence: $this::$variable
>
> At the moment there is no way to access static variables from outside
> of the class dynamically. As a workaround for this I'm currently
> creating a temporary instance (new $type()) to access them dynamically
> with a __get() function in all the derived child classes.
>
> There are ways to do it with class constants and the constant
> functions. But it's not very elegant and class constants can't hold
> arrays and/or objects.
>
> I have no idea what the implications would be. Just thought it would
> be a nice addition to the language. :) Hope I didn't overlook some
> existing PHP feature that already allows me to do this. :|
>
>
> Cheers,
>
> Bart de Boer

---

## #29803 — Bart de Boer — 2007-05-26T12:11:12+00:00

URL: https://externals.io/message/29801#29803 · In reply to: #29802

I understand... My suggestion would be to map $this to the class name 
(of the current instance) if the Scope Resolution Operator '::' is used...


Marco Kaiser wrote:
> Hi,
>
> this doesnt work because static vars are bound to his class and are not
> inherited by a child class.
> maybe this would be work with php6 or a other 5.x version.
> (Same behavior like the singleton pattern getInstance() abstract class
> stuff)
>
> -- Marco
>   
> > […]

---

## #29804 — Bart de Boer — 2007-05-26T12:36:16+00:00

URL: https://externals.io/message/29801#29804 · In reply to: #29802

I seem to have been a bit too hasty with my previous reply... You meant 
it wouldn't be able to access the static vars of the inherited Base 
class?... I think that's a different feature regarding the static access 
mechanism currently already in place... I'm only suggesting the ability 
to reference classes dynamically some way...

-- Bart


Marco Kaiser wrote:
> Hi,
> 
> this doesnt work because static vars are bound to his class and are not
> inherited by a child class.
> maybe this would be work with php6 or a other 5.x version.
> (Same behavior like the singleton pattern getInstance() abstract class
> stuff)
> 
> -- Marco
> > […]

> Hi,
> 
> this doesnt work because static vars are bound to his class and are not
> inherited by a child class.
> maybe this would be work with php6 or a other 5.x version.
> (Same behavior like the singleton pattern getInstance() abstract class
> stuff)
> 
> -- Marco
> > […]

---

## #29805 — Arnold Daniels — 2007-05-26T13:36:55+00:00

URL: https://externals.io/message/29801#29805 · In reply to: #29804

Hi,

I agree that $this::$var isn't really logical, since $this is an object 
and not a class name. But I do not see why $class::$var shouldn't work, 
or $class::$method() for that method. You can also do $function().

Also I want to suggest that brackets can be used more freely. Currently 
you can use ${$group . '_myvar'}, but you can't do the same for 
functions and classes. It would be great if you could use {$group . '_' 
. $fn}() and {get_class($this)}::$var.

The way to solve this problems right now is to use eval('return ' . 
get_class($this) . '::$var;') for getting the value. Getting a reference 
to the variable to set it, is even more messy. You need to do something 
like eval('$cvar =& ' . get_class($this) . '::$var;'); $cvar = 'bye';.

Anyway, it is not so nice, but doable in user space fairly easily. So I 
don't see why anything needs to be added in PHP 5. It would be nice to 
have a better method in PHP 6 though.

 From Holland with love,
Arnold


Bart de Boer wrote:
>
> I seem to have been a bit too hasty with my previous reply... You 
> meant it wouldn't be able to access the static vars of the inherited 
> Base class?... I think that's a different feature regarding the static 
> access mechanism currently already in place... I'm only suggesting the 
> ability to reference classes dynamically some way...
>
> -- Bart
>
>
> Marco Kaiser wrote:
> > […]
>
> > […]

---

## #29807 — Gregory Beaver — 2007-05-26T14:36:41+00:00

URL: https://externals.io/message/29801#29807 · In reply to: #29805

Arnold Daniels wrote:
> Hi,
> 
> I agree that $this::$var isn't really logical, since $this is an object
> and not a class name. But I do not see why $class::$var shouldn't work,
> or $class::$method() for that method. You can also do $function().
> 
> Also I want to suggest that brackets can be used more freely. Currently
> you can use ${$group . '_myvar'}, but you can't do the same for
> functions and classes. It would be great if you could use {$group . '_'
> . $fn}() and {get_class($this)}::$var.
> 
> The way to solve this problems right now is to use eval('return ' .
> get_class($this) . '::$var;') for getting the value. Getting a reference
> to the variable to set it, is even more messy. You need to do something
> like eval('$cvar =& ' . get_class($this) . '::$var;'); $cvar = 'bye';.
> 
> Anyway, it is not so nice, but doable in user space fairly easily. So I
> don't see why anything needs to be added in PHP 5. It would be nice to
> have a better method in PHP 6 though.
> 
> From Holland with love,
> Arnold

Hi Arnold,

<?php
$a = $group . '_' . $fn;
$a();
?>

As for get_class($this)::$var it pays to search the mailing list
archives.  the static:: keyword is being using in PHP 6 to do exactly
what you want from within a class.  Outside of a class, $var::$value
doesn't work yet, but the implementation of static may make it possible
to implement $var::$value as well.

Greg

P.S. The list archives are http://marc.info/?l=php-dev and are searchable

---

## #29815 — Arnold Daniels — 2007-05-26T18:43:44+00:00

URL: https://externals.io/message/29801#29815 · In reply to: #29807

Hi again,

I'm aware that you can do `$a = $group . '_' . $fn; $a();`, but you can 
do `$b = $group . '_' . $var; echo $$b;` as well as `echo ${$group . '_' 
. $var}`. It's a mater of style, not if there is any way to get it done.

Yes I've read about 'static::' some time ago, but forgot about it. 
Thanks for pointing it out. I (unfortunately) took that as an example, 
but it wasn't the case I wanted to make.

To my opinion, the ease with which you can use output of an expression 
to be used as name of a variable is wonderful. The code is so much 
clearer than when you have to create all kind of temporary variables 
and/or use eval. I think it is unfortunate that the same logic isn't 
available for functions and classes.

Best regards,
Arnold



Gregory Beaver wrote:
> Arnold Daniels wrote:
>   
> > […]
>
> Hi Arnold,
>
> <?php
> $a = $group . '_' . $fn;
> $a();
> ?>
>
> As for get_class($this)::$var it pays to search the mailing list
> archives.  the static:: keyword is being using in PHP 6 to do exactly
> what you want from within a class.  Outside of a class, $var::$value
> doesn't work yet, but the implementation of static may make it possible
> to implement $var::$value as well.
>
> Greg
>
> P.S. The list archives are http://marc.info/?l=php-dev and are searchable

---

## #29828 — Daniel Convissor — 2007-05-27T15:35:04+00:00

URL: https://externals.io/message/29801#29828 · In reply to: #29801

> 
> class Base {
> 	public static $var = 'hello';
> 
> 	public function someFunc() {
> 		echo self::$var; // Currently maps to Base::$var
> 		echo $this::$var; // Should map to Child::$var

Here, and throughout the example, you're mixing static and object 
contexts.  You need to pick one or the other and stick to it.  If you 
really want static, use child::$var.

--Dan

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409

---

## #29829 — Daniel Convissor — 2007-05-27T15:40:43+00:00

URL: https://externals.io/message/29801#29829 · In reply to: #29828

> 
> > […]
> 
> Here, and throughout the example, you're mixing static and object 
> contexts.  You need to pick one or the other and stick to it.  If you 
> really want static, use child::$var.

Though that's only working because the child class' name is "Child". :/

--Dan

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409

---

## #29830 — Etienne Kneuss — 2007-05-27T15:52:49+00:00

URL: https://externals.io/message/29801#29830 · In reply to: #29801

Hi,

Bart de Boer wrote:
> $class = 'Child';
>
> $obj = new $class(); // This works.
>
> echo $class::$var; // This doesn't. Should map to Child::$var
>
>
This feature request was already brought up a couple of weeks/months 
ago, please consider:

http://news.php.net/php.internals/29067

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
