Page 1 of 1

"new" operator question

Posted: Mon Jan 17, 2005 8:49 am
by kernel23
Hi guys.
I'm a little newbie to php, so I have simple question.

Php4 does not allow next code constructions:

Code: Select all

$result = (new SampleClass('x'))->sampleMethod();

Code: Select all

($result = new SampleClass('x'))->sampleMethod();
Idea in getting function result without declaring class instance variable.
Is it possible or I should use

Code: Select all

$sample = new SampleClass('x');
$result = $sample->sampleMethod();

Thank you in advance.

Posted: Mon Jan 17, 2005 8:58 am
by feyd
it would appear that you can only really do the last example. To tell the truth, it is more clear as to what you are doing then. It would be wasteful if there were multiple instantiations of the same class made with calls to functions like those exampled.

Posted: Mon Jan 17, 2005 10:19 am
by timvw
php doesn't allow us (yet) to chain method calls... so only the 2nd option is available