OOP (PHP) classes and extended classes
Just a general question:
When I program in oop and I want to have a class user and another one which is called admin, should I use inheritance for this? I mean: the user has certain capabilities but the admin has more (no kidding :) ) Is it ok to create a class "user" with a number of basic methods and then create a class "admin" which extends "user" and contains some extra methods. (it uses all the same members as the "user" class) Or should I just create "admin" class and use an instance of the "user" class as member? And can I use a protected member from the "user" class like this in the "admin" class: parent::variable ?? or is it $parent::variable ?? (like in $this->variable) Thanks for any help. regards, Lieven |
When you do something like class admin extends user {...,
if you want to access, say a $name property of user class, you just access it with $this->name in admin class, as for any method of user class like $this->login(), etc... The user class is extended with the properties and methods added by admin class |
Quote:
Is admin a user? If yes, use inheritance. Does an admin have a user? If yes, use composition. It's the good old "is a" vs "has a" question test. |
Indeed, admin is a user.
a user is not just a part of admin. thanks for the replies |
All times are GMT -5. The time now is 09:40 PM. |