You are here: Static method to get class variable (PHP4) « All PHP « IT news, forums, messages
Static method to get class variable (PHP4)

Posted by Markus Ernst on 05/31/06 10:04

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация