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
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::$variableAt 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
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
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::$variableAt 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
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 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::$variableAt 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
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::$variableAt 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
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:
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::$variableAt 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
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::$variableAt 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
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
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,
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,
ArnoldHi 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
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
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.
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
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