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
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: flopin at webgarden dot cz
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Mon Apr 29 14:01:30 2024 UTC