php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29377 can't call constructor in class definition
Submitted: 2004-07-25 17:36 UTC Modified: 2004-07-29 13:56 UTC
From: php at vladathome dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.0 OS: Linux
Private report: No CVE-ID: None
 [2004-07-25 17:36 UTC] php at vladathome dot com
Description:
------------
In most (all?) other OOP-oriented languages, it's a common paradigm to define a class and provide a static member of the class in the definition and initialize it with a new object of the class. This is commonly used to implement singletons which are a must for many projects. This doesn't seem to work  with PHP5.

Reproduce code:
---------------
<?php
class Foo {
    static $me = new Foo();
    public function __construct() {
    }
}
?>


Expected result:
----------------
Parse error: parse error, unexpected T_NEW in /u1/home/vlad/php/test/3.php on line 3

Actual result:
--------------
created static member of the class with the initialized object

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-29 13:56 UTC] tony2001@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

You cannot use an expression as value of an attribute in the class definition.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Aug 15 16:01:28 2024 UTC