php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41141 Class as static variable
Submitted: 2007-04-19 23:57 UTC Modified: 2007-04-20 08:32 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: flopin at webgarden dot cz Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 6CVS-2007-04-19 (snap) OS: any
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
27 + 35 = ?
Subscribe to this entry?

 
 [2007-04-19 23:57 UTC] flopin at webgarden dot cz
Description:
------------
It is not possible to create and assign new class instance to static variable in declaration of another class.

Reproduce code:
---------------
class A {
	public $dummy='AAA';
}

class B {
	static $foo=new A();
}


Expected result:
----------------
The variable B::$foo should contain an instance of class A, instead the parser returns error "Parse error: syntax error, unexpected T_NEW in /var/www/php6/test.php6 on line 6".

It is possible to hack the problem with code like this:

class A {
	public $dummy='AAA';
}

class B {
	static $foo=null;
	static function prepare() {
		self::$foo = new A();
	}
}
B::prepare();

but since i need to use this construct in my program widely, the code gets very crappy, buggy and unreadable



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-20 05:30 UTC] johannes@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

.
 [2007-04-20 08:26 UTC] flopin at webgarden dot cz
Of course this is not a bug, thats why i marked the report Feature/Change Request. And since I see no logical reason why this feature is not possible, it could be introduced to the language
 [2007-04-20 08:32 UTC] tony2001@php.net
>And since I see no logical reason why this feature is not possible, 
>it could be introduced to the language.
Of course there is a logical explanation - there are two stages:
1) compilation
2) execution

Classes and their static properties are declared during the compilation, no code can be run at this stage.
Objects are created during the execution.
Therefore it's not possible to use expressions when declaring class properties unless someone wants to rewrite whole engine from scratch.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC