php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21669 "$obj = new $this->var;" doesn't work
Submitted: 2003-01-15 11:56 UTC Modified: 2003-06-22 05:58 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: slowbyte at hot dot ee Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2003-01-15 (dev) OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: slowbyte at hot dot ee
New email:
PHP Version: OS:

 

 [2003-01-15 11:56 UTC] slowbyte at hot dot ee
The following snippet is a parse error in PHP5-ZE2 (used to work on earlier versions). This feature is also used in Smarty templates.

<?php
class Test {
	function say_hello() {
		echo "Hello world";
	}
}

class Factory {
	var $name = "Test";
	function create() {
		$obj = new $this->name; /* Parse error */
		return $obj;
	}
}
$factory = new Factory;
$test = $factory->create();
$test->say_hello();
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-28 19:55 UTC] yoglets at hotmail dot com
Don't know if this is the same issue or not, but it's certainly related. You can't use $obj = new $name for nested classes either. For example:

<?php

class Foo {
    class Bar {
    }
}

$obj1 = new Foo;  // works

$obj2 = new Foo::Bar;  // works

$name = 'Foo';
$obj3 = new $name;  // works

$name = 'Foo::Bar';
$obj4 = new $name;  // Fatal error: Class 'foo::bar' not found

?>
 [2003-02-18 12:51 UTC] yoglets at hotmail dot com
FWIW, this problem is still present in a post-nested-class ZE2 build:

<?php

namespace Foo
{
    class Bar
    {
    }
}

$name = 'Foo::Bar';
$obj = new $name;
print_r($obj);

?>
 [2003-03-02 05:15 UTC] sander@php.net
Verified with latest CVS.
 [2003-06-15 07:17 UTC] stas@php.net
This is due to dynamic_class_name chain of rules, which does not allow property references. Somebody with more parser knowledge (which means Zeev or Andi, I guess :) should look at it.
 [2003-06-22 05:58 UTC] zeev@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Mon Dec 17 04:01:25 2018 UTC