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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: finalvoid at gmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 12:01:28 2025 UTC