/    Sign up×
Community /Pin to ProfileBookmark

when to extend or instantiate another class?

Hi, I was wondering about when it is best practice to extend a class or best to create an object of one class within a class.

Anyone have any rules you go by?

For example:
myDatabaseClass extends mysqli{} makes sence because there’s no confusion of responsibility and it allows me to use all the mysqli functionality within my class.

But,

myPaginateClass extends myDatabaseClass{} would allow me to use myDatabaseClass functionality but would also give myPaginateClass access to mysqli.

For security, logic flow, separation of responsibility would it be better to include myDatabaseClass with a “require_once()” in the myPaginateClass and create a database object for use with the paginate functions?

Or would it be best to keep the classes totally separate, create objects of both in the main script, and use vars or arrays to pass info from one to the other?

It gets more complicated if I add yet another class for validation and data sanitizing so I want to get into the best practice and want to know how you would do it and why.

Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@iansane6authorSep 17.2011 — Been doing some reading and talking to some other people and came to the conclusion that a pagination class should not extend the db class because it only manipulates the data from the db class and the db class is only responsible for retrieving and returning the data back to the main script.

Makes sense as per the "is a" relationship rule. And if I were to apply the MVC(model, view, controller) rules I'm thinking I'll have a lot of small separate classes in my app and be doing a lot of passing data back and forth between them.
×

Success!

Help @iansane6 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...