php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #42441 Unable to instanciate a class who's name is in a constant
Submitted: 2007-08-27 10:08 UTC Modified: 2007-08-30 08:58 UTC
From: romain dot tartiere at healthgrid dot org Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.3 OS: GNU/Linux
Private report: No CVE-ID: None
 [2007-08-27 10:08 UTC] romain dot tartiere at healthgrid dot org
Description:
------------
If you use define to set the name of a class to a constant, you can't use it to instanciate the class.

Reproduce code:
---------------
<?php
class foo {}

define('MYCLASS', 'foo');
$f = new MYCLASS();                  # Won't work
$f = new $_CONSTANTS['MYCLASS']();   # Won't work

$s = MYCLASS;
$f = new $s();                       # Works
?>

Expected result:
----------------
These way of instanciating the foo class should all work.

Actual result:
--------------
The two first syntax produce the same result:
Fatal error: Class name must be a valid object or a string

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-27 10:22 UTC] jani@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-08-27 12:42 UTC] romain dot tartiere at healthgrid dot org
Thank you for your personalised answer and taking time to read and understand my bug report.

There is nothing about this behaviour in:
 - http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new
 - http://php.net/manual/en/language.constants.php

Moreover, the behaviour I describe is common to all programming languages I know (I don't pretent to know all languages, I just tell that PHP constants are not like other programming language constants).

According to me, it is just a LALR parser  mistake.

Something like...

stuff: [...]
     | TOK_NEW string '(' ')'
     | TOK_NEW variable '(' ')'

... instead of ...

stuff: [...]
     | TOK_NEW expression '(' ')'

Sorry for being rude, but your copy-paste-reply make me feel my problem has been underestimated by an inexperienced person. But maybe I am wrong, then just prove me that what I am talking about is nonsense...

According to me, if I can't do "new a" but can do "b = a; new b", there is something wrong.
 [2007-08-30 07:12 UTC] derick@php.net
A quick close doesn't mean it wasn't a correct close. This is not a bug, it's not meant to work. And don't lecture us about LALR parsers when you have no idea how PHP works.
 [2007-08-30 08:58 UTC] romain dot tartiere at healthgrid dot org
Not meant to work? haha! BTW, you may reconsider the status of bug #23022 since it is exactly the same kind of problem (parser can only cope with simple situations).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 13:01:33 2024 UTC