php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61275 Cannot have class method with same name as language construct
Submitted: 2012-03-04 17:52 UTC Modified: 2012-03-04 18:30 UTC
From: php at mralston dot com Assigned:
Status: Duplicate Package: Class/Object related
PHP Version: 5.4.0 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
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:
31 + 32 = ?
Subscribe to this entry?

 
 [2012-03-04 17:52 UTC] php at mralston dot com
Description:
------------
It isn't possible to have a class method whose name is the same as any of the 
language constructs, e.g. array(), doing so creates a parse error as follows:

PHP Parse error: parse error, expecting `T_STRING' in - on line 8

The error message is somewhat cryptic and confusing when it affects code which 
looks 
otherwise perfectly valid.

This perhaps isn't a big deal for most people as the work around is to rename your 
method. It is a problem when using a different name for the method is undesirable 
(e.g. I'm creating a framework in PHP which emulates that from a different 
programming language, so ideally the method names should be the same).

Test script:
---------------
<?php

$test=new testClass();
$test->array();

class testClass
{
	public function array()
	{
		echo("testClass::array() called.");
	}
}

?>

Expected result:
----------------
testClass::array() called.

Actual result:
--------------
PHP Parse error: parse error, expecting `T_STRING' in - on line 8

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-04 17:56 UTC] php at mralston dot com
-Package: Compile Failure +Package: Class/Object related
 [2012-03-04 17:56 UTC] php at mralston dot com
Changed package field from 'Compile Failure' to 'Class/Object related', as 
'Compile Failure' is probably intended to relate to problems compiling PHP itself, 
rather than PHP scripts.
 [2012-03-04 18:02 UTC] nikic@php.net
This is a duplicate of https://bugs.php.net/bug.php?id=28261. See there for a discussion of why this is hard to do.
 [2012-03-04 18:02 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2012-03-04 18:30 UTC] php at mralston dot com
Fair enough. I didn't spot that bug report, but I didn't really know what to 
search for when I was looking.

I don't quite understand from the other thread why resolving it is difficult, 
however as I am not intimately familiar with how PHP operates under the hood I 
am willing to accept that there are perfectly valid reasons beyond my 
understanding.

I'll +1 the other bug report. :-)

Thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 03:01:28 2024 UTC