Wuhtzu Posted August 13, 2008 Share Posted August 13, 2008 Hey As the topic says, what is the difference between ClassName::function() and $this->function() ?? The only thing I can think of / has noticed is that the first can be used in case of inheritage and the second one can't. Thanks for your input Wuhtzu Quote Link to comment https://forums.phpfreaks.com/topic/119514-difference-between-classnamefunction-and-this-function/ Share on other sites More sharing options...
matthewhaworth Posted August 13, 2008 Share Posted August 13, 2008 ClassName::function() calls a static function, which means you don't have to initialise the class to use it ClassName->function() calls a function from an object that exists Quote Link to comment https://forums.phpfreaks.com/topic/119514-difference-between-classnamefunction-and-this-function/#findComment-615702 Share on other sites More sharing options...
Wuhtzu Posted August 13, 2008 Author Share Posted August 13, 2008 Thanks, matthewhaworth. When would you use a function from a class without instantiate the class? Quote Link to comment https://forums.phpfreaks.com/topic/119514-difference-between-classnamefunction-and-this-function/#findComment-615708 Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 Thanks, matthewhaworth. When would you use a function from a class without instantiate the class? When the function doesn't have to do with a specific instantiation of a class, like a User::Load() function or something, or when using the Factory pattern, etc. Quote Link to comment https://forums.phpfreaks.com/topic/119514-difference-between-classnamefunction-and-this-function/#findComment-615714 Share on other sites More sharing options...
Wuhtzu Posted August 13, 2008 Author Share Posted August 13, 2008 Thanks DarkWater, that makes sense. So if you had a function which would just return the time or date for use in other functions you would make it static because it does not deal with any specific instance of the class? <?php static function getdate() { return date('r', time()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/119514-difference-between-classnamefunction-and-this-function/#findComment-615728 Share on other sites More sharing options...
ipoelnet Posted August 30, 2008 Share Posted August 30, 2008 sample 1 : <?php class age{ var $aTrue="true"; var $aFalse="false"; function printtrue(){ print "true"; // content manual } function printfalse(){ $this->aFalse; // content variable } } $ageB=new age; class dispAge extends age{ function dispTrue(){ print $age::printtrue(); // disp "true"; } function dispFalse(){ global $ageB; print $ageB->printfalse(); // disp "false"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/119514-difference-between-classnamefunction-and-this-function/#findComment-629621 Share on other sites More sharing options...
448191 Posted August 31, 2008 Share Posted August 31, 2008 When would you use a function from a class without instantiate the class? A class cannot be instantiated. That's like saying "I'm building this blueprint". Instead, you say "I'm building a house, using this blueprint". In OOP, you instantiate an object, using a class. Try to avoid static methods. In some cases they're warranted. In most cases, they're not. Quote Link to comment https://forums.phpfreaks.com/topic/119514-difference-between-classnamefunction-and-this-function/#findComment-630308 Share on other sites More sharing options...
deadimp Posted September 1, 2008 Share Posted September 1, 2008 Explicitly scoping a method call to a class can be used in inheritance. Say you're overloading a parent's method in a child class, but you want to still use the parent method in your child's method. If you try calling that same method as you normally would, you would just get a infinite recursion (probably resulting in stack overflow). But if you explicitly scope the method call (inside the class), the interpreter knows to call the parent's function. Example: <?php class Dad { function format($str) { return "[ $str ]"; } } class Child extends Dad { function format($str) { //Calling $this->format($str) would call this same function, but scoping it to the parent will not return "{{ " . parent::format($str) . " }}"; } } $timmy = new Child(); echo $timmy->format("something"); ?> It's like explicit scoping in C++ in that you don't put the $this-> reference (since you can't, i.e. $timmy->Dad::format("something") is apparently illegal). You call it as you would a static, but it still retains the $this reference - which can create problems with other functions if you don't declare them as 'static' functions... but that goes along with coding practices as 448191 pointed out. Probably more than you wanted know. Meh. Quote Link to comment https://forums.phpfreaks.com/topic/119514-difference-between-classnamefunction-and-this-function/#findComment-630750 Share on other sites More sharing options...
phpoet Posted September 7, 2008 Share Posted September 7, 2008 This has been a good thread. Thanks to everyone for posting about static methods and when/how to use them. I think it is a very overlooked feature in PHP and I'm glad to see it getting some publicity here. Great stuff! Quote Link to comment https://forums.phpfreaks.com/topic/119514-difference-between-classnamefunction-and-this-function/#findComment-636130 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.