|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-05-15 10:04 UTC] php at schlueters dot de
I'm running this script using different php5 snapshots and current php5-CVS-Versions:
<?php
class myclass {
var $name = 'myclass';
}
$object1 = new myclass();
$object2 = new $object1->name(); // Parse error
?>
Gives a parse error at the marked line but
<?php
$name = $object1->name;
$object2 = new $name();
?>
works.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 06 23:00:02 2026 UTC |
When using ZE2, this will indeed result in a parse error. When using ZE1, it works and the property name will be used as the class name to be instanciated. et@edea:~$ /usr/local/sources/php-4.3.1/sapi/cli/php <?php class Foo { var $bar = 'Foo'; } $foo1 = new Foo; $foo2 = new $foo1->bar; ?> et@edea:~$ /usr/local/sources/unstable/php5-200305031330/sapi/cli/php <?php class Foo { var $bar = 'Foo'; } $foo1 = new Foo; $foo2 = new $foo1->bar; Parse error: parse error in - on line 6 But i think this is the way it should be, because it looks ambiguous when you see new $object1->name($arg); - on the first look, it looks like a call to the method `name' and not like passing arguments to a constructor. Somehow, it's the same for the new $name() - it could be mistaken as a dynamic function call. Maybe there should be a function to instantiate objects dynamically? Like object create_instance(string classname[, mixed constructor_args...])