LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   OOP (PHP) classes and extended classes (https://www.linuxquestions.org/questions/programming-9/oop-php-classes-and-extended-classes-297946/)

ldp 03-05-2005 09:33 AM

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

keefaz 03-05-2005 11:00 AM

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

coolman0stress 03-05-2005 11:26 AM

Quote:

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.
Sounds good to me. A good way to quickly determine whether to use inheritance or composition is to ask the following questions:

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.

ldp 03-05-2005 11:45 AM

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.