php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23643 Parse error when instancing object from a class property
Submitted: 2003-05-15 10:04 UTC Modified: 2003-05-17 15:11 UTC
From: php at schlueters dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2003-05-15 (dev) OS: Linux 2.4.20
Private report: No CVE-ID: None
 [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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-15 12:20 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

$name will be used as the class name to be instanciated.
 [2003-05-17 15:11 UTC] et@php.net
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...])
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 17:00:01 2026 UTC