PHP objects:: method chaining efficiency!

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

PHP objects:: method chaining efficiency!

Post by jaoudestudios »

How efficient is method chaining...
Is there a greater overhead using method chaining because the object is returned in each method?

Can static methods be chained?

Code: Select all

object::method1()::method2()::method3()
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: PHP objects:: method chaining efficiency!

Post by Chris Corbyn »

jaoudestudios wrote:How efficient is method chaining...
Is there a greater overhead using method chaining because the object is returned in each method?
The overhead would be minimal, or possibly non-existent. The parser shouldn't have to do much work to make method chaining work, it's just evaluating a set of expressions (method calls) from left to right in the normal scanning routine a scanner makes. I don't know about the internals of PHP specifically though.
Can static methods be chained?

Code: Select all

object::method1()::method2()::method3()
No. You can only make static calls to a class and you can't return a class.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: PHP objects:: method chaining efficiency!

Post by jaoudestudios »

Cheers :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: PHP objects:: method chaining efficiency!

Post by Jenk »

You already can chain?

Code: Select all

$object->method()->method2()->method3()->etc();
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: PHP objects:: method chaining efficiency!

Post by Chris Corbyn »

I think we know that bit. The question was about performance (or at least, my answer was) ;)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: PHP objects:: method chaining efficiency!

Post by jaoudestudios »

It was about performance, dont know what Jenk is smoking!?! :wink:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP objects:: method chaining efficiency!

Post by josh »

Technically couldn't you return a singleton to do static chaining? Don't see the purpose, but technically?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: PHP objects:: method chaining efficiency!

Post by Chris Corbyn »

It's not really static but yeah it'd have a similar effect.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP objects:: method chaining efficiency!

Post by josh »

Yeah it would be pretty much retarded but I was just thinking out of the box :lol:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: PHP objects:: method chaining efficiency!

Post by Jenk »

jaoudestudios wrote:It was about performance, dont know what Jenk is smoking!?! :wink:
Well if you want to play that card :P

What are you smoking to think it would be any different from returning objects other than $this? :P ;)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: PHP objects:: method chaining efficiency!

Post by jaoudestudios »

Well, in my defense, I thought because $this is being returned it would have a larger overhead.
klee
Forum Newbie
Posts: 2
Joined: Mon Dec 22, 2008 9:43 am

Re: PHP objects:: method chaining efficiency!

Post by klee »

I was wonder if any one could help me. I am creating a page that allows user to insert date into mysql via check boxes. The code allow the information be inserted but does not return check values from the data on the refresh. Does any have any methods that may work?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP objects:: method chaining efficiency!

Post by John Cartwright »

klee wrote:I was wonder if any one could help me. I am creating a page that allows user to insert date into mysql via check boxes. The code allow the information be inserted but does not return check values from the data on the refresh. Does any have any methods that may work?
We will be glad to help you. However, de-railing a thread is not the way to ask for help. Kindly create your own thread and ask your question(s) there.
Post Reply