[PHP] new $foo->className(); Class name must be a valid object or a string

[PHP] new $foo->className(); Class name must be a valid object or a string

Post by stuttl » Mon, 05 May 2008 09:31:28



I would rather have a factory method that returns a new instance of
the class. There's no need for the outside world to know the class name.

<?php
class Foo
{
public function newInstance()
{
return new self();
}

public function test($a)
{
echo 'test: '.$a."\n";
}
}

$foo = new Foo;
$foo->test('foo');
$bar = $foo->newInstance();
$bar->test('bar');
?>

However, if you insist on doing it your way can I make a small
suggestion? It's better to spend your time on functionality rather
than finding ways to save some typing. I see no reason to try to
combine the two statements - saving typing and a pitiful amount of
disk space are the only benefits.

-Stut

--
http://www.yqcomputer.com/