"new" operator question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kernel23
Forum Newbie
Posts: 1
Joined: Mon Jan 17, 2005 8:30 am

"new" operator question

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

php doesn't allow us (yet) to chain method calls... so only the 2nd option is available
Post Reply