|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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();
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 12:00:01 2025 UTC |
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 ?>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); ?>