php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44026 new AnObject->method(); can't work
Submitted: 2008-02-03 01:15 UTC Modified: 2008-02-04 08:30 UTC
From: giorgio dot liscio at email dot it Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.2.5 OS: winxpsp2 / Apache 2
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-02-03 01:15 UTC] giorgio dot liscio at email dot it
Description:
------------
Hello,
i'm asking for a feature change in PHP
in java this code works nice

MyClass object = new MyClass().someMethod();

in php not too.

It is possible make working this syntax?

thank you [for creating my favourite language] :)

Reproduce code:
---------------
<?php
/* JAVA CODE:
class TestPhp
{
	public TestPhp()
		{System.out.print("Construct ");}
	public TestPhp method1()
		{System.out.print("Method1 "); return this;}
	public TestPhp method2()
		{System.out.print("Method2 "); return this;}
	public static void main(String []args)
		{TestPhp obj = new TestPhp().method1().method1();}
}
*/

class TestPhp
{
	public function __construct()
		{echo("Construct ");}
	public function method1()
		{echo("Method1 "); return $this;}
	public function method2()
		{echo("Method2 "); return $this;}
	public static main()
		{$obj = new TestPhp()->method1()->method2();}
}
TestPhp::main();

?>

Expected result:
----------------
Construct Method1 Method2

Actual result:
--------------
(Fatal Error)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-03 02:31 UTC] rasmus@php.net
As far as I am concerned this just makes the code harder to read.  What do you actually gain vs. just doing:

  $obj = new TestPhp();
  $obj->method1()->method2();

And it gets really confusing if the functions don't return $this since
then $obj wouldn't necessarily be the actual instantiated object.  I guess we can leave it as a feature request, but unless some other developer takes a liking to this, I doubt it will happen.
 [2008-02-03 02:45 UTC] giorgio dot liscio at email dot it
don't know... i think it can be useful in some cases... for example:

$a = new A();
$b = new B();
$c = new C();
$d = new D();
$e = new E();
$f = new F();
$g = new G();
$h = new H();
$i = new I();
// ... a lot of codelines that can be removed

$obj->add(
$a->method1()->method2()->method3(),
$b->method1()->method2()->method3(),
$c->method1()->method2()->method3(),
$d->method1()->method2()->method3(),
...
);

instead, with this feature, the code can be a lot shorter

$obj->add(
new A()->method1()->method2()->method3(),
new B()->method1()->method2()->method3(),
new C()->method1()->method2()->method3(),
new D()->method1()->method2()->method3(),
...
);

it is not indispensable, but i can, and my applications too, really appreciate this feature, instance objects and use methods in the same row
 [2008-02-04 08:30 UTC] derick@php.net
No need, if you really want this,m you can use a factory pattern:

$res = A::create()->method1()->method2();


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 00:01:28 2024 UTC