Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

oop

php

People also ask

How can we call a method from an object in PHP?

To invoke a method on an object, you simply call the object name followed by "->" and then call the method. Since it's a statement, you close it with a semicolon. When you are dealing with objects in PHP, the "->" is almost always used to access that object, whether it's a property or to call a method.

Can an object call a method?

Calling an object's method is similar to getting an object's variable. To call an object's method, simply append the method name to an object reference with an intervening '. ' (period), and provide any arguments to the method within enclosing parentheses.

How do you instantiate an object in PHP?

To create an Object in PHP, use the new operator to instantiate a class. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created.

What does instantiate mean in PHP?

When you instantiate a class, you create an instance of it … thus creating the object. In other words, instantiation is the process of creating an instance of an object in memory.


The feature you have asked for is available from PHP 5.4. Here is the list of new features in PHP 5.4:

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

And the relevant part from the new features list:

Class member access on instantiation has been added, e.g. (new Foo)->bar().


You cannot do what you are asking ; but you can "cheat", using the fact that, in PHP, you can have a function that has the same name as a class ; those names won't 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;
}

You can then declare a function that returns an instance of that 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 one-liner, like you asked -- only thing is you are calling the function, thus not using new :

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

And it works : here, I'm getting :

int 20

as output.


And, better, you can put some phpdoc on your function :

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

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



Edit 2010-11-30 : Just for information, a new RFC has been submitted, a few days ago, that proposes to add this feature to one of the future versions of PHP.

See : Request for Comments: Instance and method call/property access

So, maybe doing things like these will be possible in PHP 5.4 or another future version :

(new foo())->bar()
(new $foo())->bar
(new $bar->y)->x
(new foo)[0]

You can do it more universally by defining an identity function:

function identity($x) {
    return $x;
}

identity(new Obj)->method();

That way you don't need to define a function for each class.


How about:

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

:P


No, this is not possible.
You need to assign the instance to a variable before you can call any of it's methods.

If you really wan't to do this you could use a factory as ropstah suggests:

class ObjFactory{
  public static function newObj(){
      return new Obj();
  }
}
ObjFactory::newObj()->method();

Simply we can do this

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

You could use a static factory method to produce the object:

ObjectFactory::NewObj()->method();