Reply to Re: [PHP] PHP5 Static Object Function Issue

Your name:

Reply:


Posted by Jochem Maas on 03/02/05 04:10

Gavin Roy wrote:
> I believe there is small problem in PHP5 with regard to static
> functions in uninstanced classes. From discussions in various php irc
> channels and what I've read about it in the docs, I can not call a
> static function in an uninstanced class via a variable. For example:
>
> $class = "MyClass";
> $instance = new $class();
>
> works, but
>
> $class = "MyClass";
> $instance = $class::getInstance();

$func = array($class,'getInstance');
if (is_callable($func)) {
$instance = call_user_func($func);
}

----

if the class is only used in the singleton context then
why not only expose static public methods - let the class deal
with the instance internally:

class Test
{
protected function __construct() {}
protected static function getInstance() { /* do stuff then... */ return $instance; }
protected function _foo() { echo "say aaaarrrhhh.\n"; }

static public function foo()
{
$x = self::getInstance();
$x->_foo();
}
}


Test::foo();

$func = array('Test','foo');
if (is_callable($func)) {
$rtnVal = call_user_func($func);
}

see also http://php.net/call_user_func_array

>
> doesnt, where
>
> $instance = MyClass::getInstance();
>
> does.
>
> I know that some people might consider it poor design, but basically
> I'm trying to dynamically reference singleton patterned classes using
> getInstance to internally create an object instance and use that
> instance for all processing. As of right now it doesn't seem that it
> is possible. Is there a reason why it's not supported? Will it be
> supported in the future?
>
> Gavin
>

[Back to original 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

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