aabid Posted March 31, 2011 Share Posted March 31, 2011 I just came up with a line of code that I am not able to understand what's it doing. The code is: function cost() { return $this->costStrategy->cost($this); } here costStrategy is a class property which holds a reference to the object of type CostStrategy class and cost() is a method defined in CostStrategy. Quote Link to comment https://forums.phpfreaks.com/topic/232268-what-does-this-syntax-mean-in-php-do-you-know/ Share on other sites More sharing options...
runthis Posted March 31, 2011 Share Posted March 31, 2011 So your saying it looks like class costStrategy{ function cost() { return $this->costStrategy->cost($this); } } ? instead of returning the value, try printing it and see what it says or what it affects Quote Link to comment https://forums.phpfreaks.com/topic/232268-what-does-this-syntax-mean-in-php-do-you-know/#findComment-1194836 Share on other sites More sharing options...
salathe Posted March 31, 2011 Share Posted March 31, 2011 From your description it sounds like you've got all of the pieces clear in your head. What in particular are you having trouble understanding? Quote Link to comment https://forums.phpfreaks.com/topic/232268-what-does-this-syntax-mean-in-php-do-you-know/#findComment-1194837 Share on other sites More sharing options...
aabid Posted March 31, 2011 Author Share Posted March 31, 2011 return $this->costStrategy->cost($this); what does "cost($this)" means in above line ?? Quote Link to comment https://forums.phpfreaks.com/topic/232268-what-does-this-syntax-mean-in-php-do-you-know/#findComment-1194839 Share on other sites More sharing options...
aabid Posted March 31, 2011 Author Share Posted March 31, 2011 not able to get it yet also, searched but not found anything which clear my doubts Quote Link to comment https://forums.phpfreaks.com/topic/232268-what-does-this-syntax-mean-in-php-do-you-know/#findComment-1194887 Share on other sites More sharing options...
salathe Posted March 31, 2011 Share Posted March 31, 2011 cost($this) is calling the cost method of the $this->costStrategy object (an instance of the CostStrategy class), and passing along the current object ($this, see docs)) as the first and only argument to that method. Quote Link to comment https://forums.phpfreaks.com/topic/232268-what-does-this-syntax-mean-in-php-do-you-know/#findComment-1194889 Share on other sites More sharing options...
KevinM1 Posted March 31, 2011 Share Posted March 31, 2011 In other words, objects can pass themselves as method parameters. Pretty wild, huh? Quote Link to comment https://forums.phpfreaks.com/topic/232268-what-does-this-syntax-mean-in-php-do-you-know/#findComment-1194893 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.