LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2005, 09:33 AM   #1
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Rep: Reputation: 18
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

Last edited by ldp; 03-05-2005 at 10:08 AM.
 
Old 03-05-2005, 11:00 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
Old 03-05-2005, 11:26 AM   #3
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
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.
 
Old 03-05-2005, 11:45 AM   #4
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 18
Indeed, admin is a user.
a user is not just a part of admin.

thanks for the replies
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes in VC++ Diederick Programming 2 11-30-2005 10:57 AM
Possible uses for classes. RHLinuxGUY Programming 4 11-21-2005 10:10 PM
classes in C++ niteshadw Programming 2 07-05-2005 07:05 PM
php classes, arrays towlie Programming 10 06-07-2005 11:10 AM
PHP and classes Boby Programming 5 03-23-2005 05:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration