sfc Posted July 8, 2010 Share Posted July 8, 2010 If I have a class DatabaseObject that has the follow method: protected function attribute () { return get_object_vars($this); } And I call this method from the class user which extends the DatabaseObject class (i.e. $user->attribute), what will "$this" refer to? The parent or the child? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/207143-inheritance-question-quick-and-easy-i-promise/ Share on other sites More sharing options...
AbraCadaver Posted July 8, 2010 Share Posted July 8, 2010 $this refers to whatever instance you are operating on: $db = new DatabaseObject; $var1 = $db->attribute(); // $this refers to the $db instance $db = new UserObject; $var2 = $user->attribute(); // $this refers to the $user instance Quote Link to comment https://forums.phpfreaks.com/topic/207143-inheritance-question-quick-and-easy-i-promise/#findComment-1083111 Share on other sites More sharing options...
sfc Posted July 8, 2010 Author Share Posted July 8, 2010 That's exactly what I want it to do. If I make the method static, will that change? Quote Link to comment https://forums.phpfreaks.com/topic/207143-inheritance-question-quick-and-easy-i-promise/#findComment-1083114 Share on other sites More sharing options...
Mchl Posted July 8, 2010 Share Posted July 8, 2010 Yes it will. 1. There's no $this in static context, you'll need to use self:: 2. self:: always refers to a class where the method was defined Quote Link to comment https://forums.phpfreaks.com/topic/207143-inheritance-question-quick-and-easy-i-promise/#findComment-1083120 Share on other sites More sharing options...
sfc Posted July 8, 2010 Author Share Posted July 8, 2010 Thanks guys...still wrapping my mind around all this. Quote Link to comment https://forums.phpfreaks.com/topic/207143-inheritance-question-quick-and-easy-i-promise/#findComment-1083121 Share on other sites More sharing options...
Mchl Posted July 8, 2010 Share Posted July 8, 2010 And of course there's no point in using get_object_vars on a class Returns an associative array of defined object accessible non-static properties for the specified object in scope. If a property have not been assigned a value, it will be returned with a NULL value. Quote Link to comment https://forums.phpfreaks.com/topic/207143-inheritance-question-quick-and-easy-i-promise/#findComment-1083123 Share on other sites More sharing options...
sfc Posted July 8, 2010 Author Share Posted July 8, 2010 So I am a little confused. The user class extends databaseobject so I should be able to call a protected class from the instantiated child right? i.e. $user->save(); Where save is protected method in the databaseobject class. But when I just did I got an error saying that I called a protected method. As soon as I change the save() method to public it worked perfectly. Can someone explain? Do you need more information? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/207143-inheritance-question-quick-and-easy-i-promise/#findComment-1083195 Share on other sites More sharing options...
AbraCadaver Posted July 8, 2010 Share Posted July 8, 2010 In this case you want private. Check this: http://us3.php.net/manual/en/language.oop5.visibility.php Quote Link to comment https://forums.phpfreaks.com/topic/207143-inheritance-question-quick-and-easy-i-promise/#findComment-1083222 Share on other sites More sharing options...
sfc Posted July 8, 2010 Author Share Posted July 8, 2010 I thought private methods couldn't be called by child classes and protected could but not by anything outside the class or sub-classes. Am I wrong? Quote Link to comment https://forums.phpfreaks.com/topic/207143-inheritance-question-quick-and-easy-i-promise/#findComment-1083267 Share on other sites More sharing options...
Alex Posted July 8, 2010 Share Posted July 8, 2010 You got that error because you're attempting to call it publicly. When a method or property is protected it means you can access it from within the child class, not outside of it. Quote Link to comment https://forums.phpfreaks.com/topic/207143-inheritance-question-quick-and-easy-i-promise/#findComment-1083270 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.