livethedead Posted March 8, 2012 Share Posted March 8, 2012 I just started my journey into OOP, just looking for a simple explanation as to why the script echo's nothing. (last line) <?php class Dog { public $name; public function bark() { echo "{$this->name} says Woof!"; } } class Poodle extends Dog { public function bark() { echo "Yip"; } } $furball = new Poodle; $furball->name = "furball"; $furball->Dog::bark(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/258544-scope-resolution-operator/ Share on other sites More sharing options...
requinix Posted March 8, 2012 Share Posted March 8, 2012 $furball->Dog::bark(); That's a syntax error. You cannot use $furball to call Dog's bark() method. You can, however, put something in Poodle that will. But what (I think) you're trying to do should look more like class Dog { public $name; public function bark() { echo "{$this->name} says {$this->getBarkNoise()}!"; } protected function getBarkNoise() { return "Woof"; } } class Poodle extends Dog { protected function getBarkNoise() { return "Yip"; } } $furball = new Poodle(); $furball->name = "furball"; $furball->bark(); Quote Link to comment https://forums.phpfreaks.com/topic/258544-scope-resolution-operator/#findComment-1325308 Share on other sites More sharing options...
livethedead Posted March 8, 2012 Author Share Posted March 8, 2012 I'm not really trying to do anything per-say, just fiddling to understand. Thanks for taking the time though ^^. I still don't understand why that would be a syntax error, since Poodle extends Dog, why can't I use :: to call bark() from the parent class? Sorry for the original question being so vague. Quote Link to comment https://forums.phpfreaks.com/topic/258544-scope-resolution-operator/#findComment-1325312 Share on other sites More sharing options...
requinix Posted March 8, 2012 Share Posted March 8, 2012 Because PHP doesn't let you do that. And it kinda violates object-oriented design. Poodle has chosen to hide Dog's bark() method with its own implementation for whatever reason (that you may or may not know about). You are not allowed to "go around" that and call Dog::bark() instead. Quote Link to comment https://forums.phpfreaks.com/topic/258544-scope-resolution-operator/#findComment-1325314 Share on other sites More sharing options...
livethedead Posted March 8, 2012 Author Share Posted March 8, 2012 Ah, I thought that was the purpose of :: I'll do more research, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/258544-scope-resolution-operator/#findComment-1325315 Share on other sites More sharing options...
batwimp Posted March 8, 2012 Share Posted March 8, 2012 $furball is an instance of Poodle, not Dog. You can't call parent fuctions (that I know of) directly from an instance of a child function. If you wanted to add this functionality to the object, you can do this: class Poodle extends Dog { public function bark() { echo "Yip"; } public function callParent(){ parent::bark(); } } And then do a $furball->callParent(). Quote Link to comment https://forums.phpfreaks.com/topic/258544-scope-resolution-operator/#findComment-1325316 Share on other sites More sharing options...
livethedead Posted March 8, 2012 Author Share Posted March 8, 2012 Alright, now I understand how I was going against OOP principles, thanks much. Quote Link to comment https://forums.phpfreaks.com/topic/258544-scope-resolution-operator/#findComment-1325318 Share on other sites More sharing options...
batwimp Posted March 8, 2012 Share Posted March 8, 2012 From outside a class, the scope resolution operator is mostly used to call static functions (functions inside a class can be called without instantiating an object of the class). So if you wanted to add a static function, you could do this: class Poodle extends Dog { public function bark() { echo "Yip"; } public function callParent(){ parent::bark(); } Public static function callMe(){ echo "Meow!"; } } Poodle::callMe(); Notice that I didn't need to instantiate an actual object to call the static function. Quote Link to comment https://forums.phpfreaks.com/topic/258544-scope-resolution-operator/#findComment-1325319 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.