BahBah Posted December 19, 2009 Share Posted December 19, 2009 Hi I'm trying to build my own User class. It is going ok so far, but I am struggling with inheritence. class user { private $profile = array { 'userID', 'firstname', 'surname' }; } class primaryUser extends user { private $additionalFields = array{ 'a', 'b' }; } A primary user is intended to have all of the properties and access to user's methods. However I want to extend the $profile array in user to incorporate the $additionalFields. I am unsure how to do this, and if I am even going about this the right way ? I was intending to go further by extending the user class for any other special user circumstances. The $profile array is used to keep track of what fields are required for a user to be submitted to the database. I hope this makes sense. Thanks for any help you can offer me. Quote Link to comment https://forums.phpfreaks.com/topic/185696-learning-php-classes/ Share on other sites More sharing options...
Alex Posted December 19, 2009 Share Posted December 19, 2009 First off, if you're using the keyword private in your user class any class that extends it will not inherit it. Instead, for this purpose, you should use protected. To add to the profile array in the classes that extend user you can user array_merge. Example: class user { protected $profile = array ('userID', 'firstname', 'surname'); } class primaryUser extends user { public function __construct() { $this->profile = array_merge($this->profile, array ('a', 'b')); print_r($this->profile); } } Output: Array ( [0] => userID [1] => firstname [2] => surname [3] => a [4] => b ) Quote Link to comment https://forums.phpfreaks.com/topic/185696-learning-php-classes/#findComment-980546 Share on other sites More sharing options...
Buddski Posted December 19, 2009 Share Posted December 19, 2009 <?php class user { protected $profile = array('userID', 'firstname', 'surname'); } class primaryUser extends user { private $additionalFields = array('a', 'b'); function __construct() { $this->profile = array_merge($this->profile,$this->additionalFields); } function output() { echo '<pre>'; print_r($this->profile); echo '</pre>'; } } $user = new primaryUser(); $user->output(); ?> Edit: Was beaten to it Quote Link to comment https://forums.phpfreaks.com/topic/185696-learning-php-classes/#findComment-980548 Share on other sites More sharing options...
BahBah Posted December 19, 2009 Author Share Posted December 19, 2009 Thank you both Instead, for this purpose, you should use protected. Does this mean that if I: $user = new primaryUser(); will all the methods in the user class still function correctly. But if I were to make some methods that affect/use the methods or data in the user class I would need to make the methods and variables protected ? Thanks for your assistance. Quote Link to comment https://forums.phpfreaks.com/topic/185696-learning-php-classes/#findComment-980549 Share on other sites More sharing options...
Alex Posted December 19, 2009 Share Posted December 19, 2009 Check out this page Basically, if the methods are public/protected they will be inherited, but if they're private they will not. Quote Link to comment https://forums.phpfreaks.com/topic/185696-learning-php-classes/#findComment-980552 Share on other sites More sharing options...
BahBah Posted December 20, 2009 Author Share Posted December 20, 2009 Thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/185696-learning-php-classes/#findComment-981023 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.