In PHP, Can You Instantiate an Object and Call a Method on the Same Line

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

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

https://php-legacy-docs.zend.com/manual/php5/en/migration54.new-features

And the relevant part from the new features list:

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

Instantiate and call a method in one line

This is not possible. You could, however, create a static method returning a new instance. Something like:

class User {
public static function create() {
return new self();
}
}

User::create()->get_name();

Is it possible to create instance and call method in one command (on the same line) in PHP?

Previously answered:

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

PHP Combine Class Definition & Class Method Call into one line

From version 5.4 you can use:

$v = (new A())->b();

(Don't miss the brackets around the constructor call)

Or in your case:

(new preferences())
->updatePreferences('prefer_CM_or_IN',$joinPrefer_CM_or_IN);

In former versions of PHP there wasn't a syntax feature than could be used to chain a constructor and a method call

Instancing an object and calling a method in one line?

No, its not possible. There is a RFC for that

http://wiki.php.net/rfc/instance-method-call

But no one knows, when this will come to the userland.

Jacob mentioned the static method. There are other more or less useful methods to achieve the same

function instanciate($className, $arg1 = null) {
$args = func_get_args();
array_shift($args);
$c = new ReflectionClass($className);
return $c->newInstanceArgs($c);
}
instanciate('Classname', 1, 2, 3)->doSomething();

However, I prefer the temporary variable (like in the question).

Update:
I can swear there where an example for the temporary variable stuff in the question in the past. However, I meant this

$x = new Class;
$x->method();

where $x is the temporary variable.

Can´t call few times a method same object PHP

You're doing a require_once() call, which is your problem. You will not re-include the file the second time it's called, but being that you're not pulling a global variable, whatever was returned from the first include is not available to you the second time around.

If it's okay to include the file multiple times, you can make the require_once a plain require and that will set your variable as you are expecting.

How to call a method after another method in one line

This syntax is called method chaining, and it's possible because each method returns the object itself ($this). This is not necessarily always the case, it's also used to retrieve an object's property that in turn also can be an object (which can have properties that are objects, and so on).

It is used to reduce the amount of lines that you need to write code on. Compare these two snippets:

Without chaining

$object->foo("Text");
$object->anotherFoo();
$object->->bar("Aloha");

Using method chaining

$object->foo("Text")->anotherFoo()->bar("Aloha");

Instantiate Class and Declare Variables in One Line

If you want to feed your own values, use your constructor to setup the properties in the arguments:

class Gallery 
{
public $image;
public $text;
public $heading;
public $link;

public function __construct($image, $text, $heading, $link) // arguments
{
$this->image = $image;
$this->text = $text;
$this->heading = $heading;
$this->link = $link;
}

public function Item()
// rest of your codes

So that when you instantiate, just fill up the arguments:

$gallery = new Gallery('img/test.jpg', 'test', 'text', 'link#');
$gallery->Item(); // echoes the html markup,

Calling method directly after new?

Not until PHP 5.4, no. In PHP 5.3 and earlier, you'll have to use another variable:

$obj = new CEntry( new Control() );
$obj->actuate();

How to make multiple calls to class methods in the same line?

The way of calling function of class one by one just by "->" because the function returning the same object of the class. See the example below. You will get this

class Wke {

public $type;
public $errno;
public $msg;
public $page;

public $template = $this;

public function notify(){
return $this;
}

public function errorno($error){
$this->errno = $error;
return $this; // returning same object so you can call the another function in sequence by just ->
}
public function type($type){
$this->type = $type;
return $this;
}
public function msg($msg){
$this->msg = $msg;
return $this;
}
public function page($page){
$this->page = $page;
return $this;
}
}

The whole magic is of return $this;



Related Topics



Leave a reply



Submit