php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40300 Calling a function on a newly created object doesn't work
Submitted: 2007-02-01 00:28 UTC Modified: 2007-02-01 09:27 UTC
From: finalvoid at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.0 OS: Windows XP
Private report: No CVE-ID: None
 [2007-02-01 00:28 UTC] finalvoid at gmail dot com
Description:
------------
Calling a function on a newly created object requires saving the object to a variable.  Script that clarifies what I'm trying to say:

class C
{  public function __construct() {}
   public function f()   {   return 1234;   }
};
$y = (new C())->f();
print($y);

The above doesn't work.  However, it does work if you replace the penultimate line with
$x = new C();
$y = $x->f();

If we add parentheses around the $x, i.e.
$y = ($x)->f();
then it still doesn't work.  This and bug report #9587 indicate that the actual underlying cause is that object-valued variables are implemented, but object-valued expressions are not.

Although the "this is not a bug" comment on the above-cited bug report #9587 did not include a rationale, it's difficult for me to decide whether it applies here as well; maybe this bug report should actually be a feature request?  Personally, as a PHP rookie but Java veteran, I would say that object-valued expressions SHOULD work, and they DON'T, therefore it's a BUG.

Thanks!

Reproduce code:
---------------
class C
{  public function __construct() {}
   public function f()   {   return 1234;   }
};
$y = (new C())->f();
print($y);

Expected result:
----------------
1234

Actual result:
--------------
Parse error: parse error, unexpected T_OBJECT_OPERATOR in c:\path\test.php on line 5


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-01 09:27 UTC] derick@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

This syntax is simply not supported.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 11:01:28 2024 UTC