php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44347 $foo[x] = y; syntax doesn't work inside a class
Submitted: 2008-03-05 23:35 UTC Modified: 2008-03-06 00:52 UTC
From: rewilliams at newtekit dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.5 OS: OS X 10.2.5
Private report: No CVE-ID: None
 [2008-03-05 23:35 UTC] rewilliams at newtekit dot com
Description:
------------
If you try to use this syntax in a class to declare a class member:

  $foo[1] = 1;

You get a parse error. Prepending a visibility keyword doesn't change 
anything, nor does using a string key instead of a numeric key.

Note: this was seen in v5.2.4. I don't have 5.2.5 to test against. I 
did, however, test it against a couple of 5.0.x versions, where I also 
saw a parse error, so this is definitely a multi-version issue.

Reproduce code:
---------------
File test.php

<?php

class foo {
	$bar[1] = 1;
}

?>


Expected result:
----------------
I'd expect $bar[1] to be created as member of class foo and assigned the 
value of 1. This syntax isn't explicitly covered in the classes section 
of the docs as far as I can see; the only restriction that's given is 
this:

"The default value must be a constant expression, not (for example) a 
variable, a class member or a function call."

Since I'm assigning a constant value, since this array assignment syntax 
is otherwise valid, and since the array() syntax works, I'd expect this 
syntax to work, as well.

Actual result:
--------------
% php -l test.php 

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION 
in test.php on line 4
Errors parsing test.php






Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-06 00:52 UTC] felipe@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

See http://docs.php.net/manual/en/language.oop5.basic.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC