469,304 Members | 2,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

Static method to get class variable (PHP4)

Hi

In order to make static class variables accessible in a clean way I try
to write a method in the base class, as shown in this example:

<?php
class baseclass
{
function get_static($varname) {
return $$varname; // here's my problem
}
}

class childclass extends baseclass
{
var $types = array('text', 'password', 'hidden');
}

print_r(childclass::get_static('types'));
?>

This throws a notice:
-> Notice: Undefined variable: types in test.php on line 5

Some modifications I tried on the return statement:

return $this->$varname;
-> Notice: Undefined property: this in test.php on line 5

return self::$varname;
return self::$$varname;
return self::${$varname};
-> Parse error: parse error, unexpected ';', expecting '(' in test.php
on line 5

I encountered that the same errors occur if no inheritance is used, this
does not work either (with the same modifications as above):

<?php
class childclass
{
var $types = array('text', 'password', 'hidden');
function get_types() {
return $types;
}
}

print_r(childclass::get_types());
?>

I wonder if I try to do something inappropriate, or if there is just
something I am missing?

--
Markus
May 31 '06 #1
4 12177

Markus Ernst wrote:
In order to make static class variables accessible in a clean way I try
to write a method in the base class, as shown in this example:
You probably mean private/protected instead of static.
<?php
class childclass
{
var $types = array('text', 'password', 'hidden');
function get_types() {
return $types;
}
}

print_r(childclass::get_types());
?>


The variable $types does not exist until an instance of this class is
made:

$foo = new childclass();
$foo->get_types();

May 31 '06 #2
Sjoerd schrieb:
The variable $types does not exist until an instance of this class is
made:


Thank you - so I misunderstood the concept of static variables; I
assumed that if methods can be called statically this was also possible
for properties.

--
Markus
May 31 '06 #3

"Markus Ernst" <derernst@NO#SP#AMgmx.ch> wrote in message
news:44**********@news.cybercity.ch...
| Sjoerd schrieb:
| > The variable $types does not exist until an instance of this class is
| > made:
|
| Thank you - so I misunderstood the concept of static variables; I
| assumed that if methods can be called statically this was also possible
| for properties.

no, i think you understand them correctly. however, ifaik, they are only
supported starting in php 5. sjoerd's post is appropriate for php < 5.

hth,

me
May 31 '06 #4
no
Sjoerd wrote:
Markus Ernst wrote:
In order to make static class variables accessible in a clean way I try
to write a method in the base class, as shown in this example:


You probably mean private/protected instead of static.
<?php
class childclass
{
var $types = array('text', 'password', 'hidden');
function get_types() {
return $types;
}
}

print_r(childclass::get_types());
?>


The variable $types does not exist until an instance of this class is
made:

$foo = new childclass();
$foo->get_types();


anyway, the code from above will still not work until you change
function to return:

function get_types() {
return $this -> types;
}
May 31 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by James | last post: by
7 posts views Thread by Edward Diener | last post: by
44 posts views Thread by John A. Bailo | last post: by
37 posts views Thread by minkoo.seo | last post: by
reply views Thread by Joe Strout | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.