# RFC: Instantiating a class and calling methods/accessing properties on same command

- Status: Implemented (PHP 5.4)
- Authors: Felipe Pena felipe@php.net
- Date: 2010-11-26
- Version: 1.0
- Original: https://wiki.php.net/rfc/instance-method-call
- Page: https://externals.io/rfc/instance-method-call
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Discussions on the mailing list

- [\[RFC\] new foo()->bar()](https://externals.io/message/50592.md) — started on 2010-11-26, 34 messages

---

## Introduction

The purpose of RFC is to presents the support for instantiating a class and calling its methods and accessing its properties on same command. We could use one of two syntaxes below:

## Syntax 1 (without brackets)

- new foo-&gt;bar() should be read as (new foo)-&gt;bar()
- new $foo()-&gt;bar should be read as (new $foo())-&gt;bar
- new $bar-&gt;y()-&gt;x should be read as (new ($bar-&gt;y)())-&gt;x

## Syntax 2 (with brackets)

- (new foo())-&gt;bar()
- (new $foo())-&gt;bar
- (new $bar-&gt;y)-&gt;x
- (new foo)\[0\]

## Examples

#### Using brackets

```php
<?php
 
class foo extends ArrayObject {
	public function __construct($arr) {
		parent::__construct($arr);
	}
}
 
var_dump( (new foo( array(1, array(4, 5), 3) ))[1][0] ); // int(4)
 
?>
```

```php
<?php
 
class foo {
	public $x = 1;
}
 
class bar {
	public $y = 'foo';
}
 
$x = 'bar';
 
$bar = new bar;
 
var_dump((new bar)->y);     // foo
var_dump((new $x)->y);      // foo
var_dump((new $bar->y)->x); // 1
 
?>
```

```php
<?php
 
class bar {
	public $z;
 
	public function __construct() {
		$this->z = new stdclass;
	}
	public function getZ() {
		return $this->z;
	}
}
 
var_dump(clone (new bar)->z);
var_dump(clone (new bar)->getZ());
 
?>
```

#### Without brackets

```php
<?php
 
class foo {
	public $x = 1;
 
	public function getX() {
		return $this->x;
	}
	public function setX($val) {
		$this->x = $val;
		return $this;
	}
}
 
$X = new foo->setX(10)->getX();
var_dump($X); // int(10)
 
?>
```

```php
<?php
 
class foo {
	public $x = 'testing';
 
	public function bar() {
		return "foo";
	}
	public function baz() {
		return new self;
	}
	static function xyz() {
	}
}
 
var_dump(new foo()->bar());               // string(3) "foo"
var_dump(new foo()->baz()->x);            // string(7) "testing"
var_dump(new foo()->baz()->baz()->bar()); // string(3) "foo"
var_dump(new foo()->xyz());               // NULL
new foo()->www();                         // Fatal error: Call to undefined method foo::www() 
 
?>
```

```php
<?php
 
class foo {
	public function __construct() {
		throw new Exception('foobar');
	}
}
 
try {
	$X = new foo->Inexistent(3);
} catch (Exception $e) {
	var_dump($e->getMessage()); // foobar
}
 
?>
```

## Patch

- <http://felipe.ath.cx/diff/instance-method-call.patch> (without brackets)
- <http://felipe.ath.cx/diff/instance-method-call-2.patch> (with brackets)
- <http://felipe.ath.cx/diff/instance-method-call-3.patch> (with brackets + array dereferencing)

## Changelog

- 08/04/2014 - Closed as “implemented”
- 06/11/2011 - Implemented (Syntax 2) in [ff48763f4b](http://git.php.net/?p=php-src.git;a=commit;h=ff48763f4b0fee906293815b42fe3e2a167702c8) (PHP 5.4.0)
- 26/11/2010 - Posted RFC on internals
- 27/11/2010 - New syntax proposed
- 29/11/2010 - Added array dereferencing support (e.g. (new foo)\[0\])
