play_ Posted December 3, 2011 Share Posted December 3, 2011 What would be the simplest way? Do you use a method in the child class? Personally I use parent::$this->whatever; But I was wondering what you guys do. Quote Link to comment https://forums.phpfreaks.com/topic/252415-ways-to-get-parent-class-variables/ Share on other sites More sharing options...
xyph Posted December 4, 2011 Share Posted December 4, 2011 That's the correct way. Personally, I don't use inheritance often. I prefer using interfaces, and other ways off class interaction. Quote Link to comment https://forums.phpfreaks.com/topic/252415-ways-to-get-parent-class-variables/#findComment-1294118 Share on other sites More sharing options...
scootstah Posted December 4, 2011 Share Posted December 4, 2011 Why not just do $this->var? Quote Link to comment https://forums.phpfreaks.com/topic/252415-ways-to-get-parent-class-variables/#findComment-1294136 Share on other sites More sharing options...
trq Posted December 4, 2011 Share Posted December 4, 2011 AS scootstah has pointed out a parents properties are available as if they are defined within the child class. parent::$this->whatever; Does make allot of sense. Maybe show us a simple example of what it is your referring to because really, to access a parents property is no different than accessing any other property. eg; $this->var; Quote Link to comment https://forums.phpfreaks.com/topic/252415-ways-to-get-parent-class-variables/#findComment-1294196 Share on other sites More sharing options...
KevinM1 Posted December 4, 2011 Share Posted December 4, 2011 Caveat: a parent's variables (and functions) are available to their children if they're marked as public or protected. Private members aren't shared. Quote Link to comment https://forums.phpfreaks.com/topic/252415-ways-to-get-parent-class-variables/#findComment-1294203 Share on other sites More sharing options...
trq Posted December 4, 2011 Share Posted December 4, 2011 I assumed that was obvious. Quote Link to comment https://forums.phpfreaks.com/topic/252415-ways-to-get-parent-class-variables/#findComment-1294411 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.