In PHP, can you instantiate an object and call a method on one line?

What I would like to do is something like this:

$method_result = new Obj()->method(); 

Instead of doing:

 $obj = new Obj(); $method_result = $obj->method(); 

The result does not really matter to me in my particular case. But is there a way to do this?

+112
oop php
Sep 09 '09 at 22:38
source share
8 answers

The function you requested is available in PHP 5.4. Here is a list of new features in PHP 5.4:

http://php.net/manual/en/migration54.new-features.php

And the corresponding part from the list of new functions:

Added access to class members when creating an instance, for example (new Foo) โ†’ bar ().

+148
Apr 09 2018-12-12T00:
source share

You cannot do what you ask; but you can "cheat" using the fact that in PHP you may have a function with the same name as the class; these names will not conflict.

So, if you declared a class like this:

 class Test { public function __construct($param) { $this->_var = $param; } public function myMethod() { return $this->_var * 2; } protected $_var; } 

Then you can declare a function that returns an instance of this class and has exactly the same name as the class:

 function Test($param) { return new Test($param); } 

And now it becomes possible to use a single line, as you requested - the only thing you call the function, that is, do not use new:

 $a = Test(10)->myMethod(); var_dump($a); 

And it works: here, I get:

 int 20 

as a conclusion.


And, better, you can add phpdoc to your function:

 /** * @return Test */ function Test($param) { return new Test($param); } 

That way, you'll even have hints in your IDE - at least with Eclipse PDT 2.x; see screenshot:

Fcnrr.png





Edit 2010-11-30: Just for information, a new RFC was introduced a few days ago, which suggests adding this function to one of the future versions of PHP.

See: Requesting Comments: Instance and Method Call / Property Access

So maybe something like this will be possible in PHP 5.4 or another future version:

 (new foo())->bar() (new $foo())->bar (new $bar->y)->x (new foo)[0] 
+37
Sep 09 '09 at 22:44
source share

What about:

 $obj = new Obj(); $method_result = $obj->method(); // ? 

: R

+19
Sep 09 '09 at 10:40
source share

You can do this more universally by defining an identification function:

 function identity($x) { return $x; } identity(new Obj)->method(); 

Thus, you do not need to define a function for each class.

+18
Jul 13 2018-11-11T00:
source share

No, It is Immpossible.
You need to assign an instance to a variable before you can call any of its methods.

If you really havenโ€™t done this, you can use the factory, as ropstah suggests:

 class ObjFactory{ public static function newObj(){ return new Obj(); } } ObjFactory::newObj()->method(); 
+15
Sep 09 '09 at 22:38
source share

You can use the static factory method to create an object:

 ObjectFactory::NewObj()->method(); 
+6
Sep 09 '09 at 10:40
source share

I was also looking for one-line to execute this as part of one expression to convert dates from one format to another. I like to do this in one line of code because it is a single logical operation. So, this is a little cryptic, but allows you to create and use a date object on one line:

 $newDateString = ($d = new DateTime('2011-08-30') ? $d->format('F d, Y') : ''); 

Another way of single-line conversion of date strings from one format to another is to use an auxiliary function to control parts of OO code:

 function convertDate($oldDateString,$newDateFormatString) { $d = new DateTime($oldDateString); return $d->format($newDateFormatString); } $myNewDate = convertDate($myOldDate,'F d, Y'); 

I think the object-oriented approach is cool and necessary, but sometimes it can be tedious, requiring too many steps to perform simple operations.

+3
Sep 01 '11 at 15:48
source share

I see that this is pretty old as the questions go, but here is something that I think should be mentioned:

A special class method called "__call ()" can be used to create new elements inside the class. You use it as follows:

 <?php class test { function __call($func,$args) { echo "I am here - $func\n"; } } $a = new test(); $a->new( "My new class" ); ?> 

The conclusion should be:

 I am here - new 

This way you can turn PHP into creating a โ€œnewโ€ command inside your top-level class (or any class) and include your include command in the __call () function to include the class you requested. Of course, you probably want to check $ func to make sure that this is the โ€œnewโ€ command that was sent to the __call () command, and (of course) you might have other commands because of how __call works ().

0
Jan 07 '16 at 6:27
source share



All Articles