/    Sign up×
Community /Pin to ProfileBookmark

using multiple classes

i want to have a class (usually the app) call many different classes to help it run.

is the best way to have the app class create an instance of each of the sub classes? or i could i have the app class extend all of the sub classes? or is there a better way of handling this?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@Reli4ntSep 19.2006 — it really depends on the situation and your needs.

A child class can access the parent classes methods and properties. In terms of logic and inuitive design you need to decide on a case by case basis.
Copy linkTweet thisAlerts:
@JDM71488authorSep 19.2006 — well, moreso of having a single class extend others. based on: [url=http://www.phpfreaks.com/forums/index.php?topic=107261.msg430133]this thread from phpfreaks[/url] i know that i can extend classes in layers like "a extends b extends c."

but i am meaning more like "a extends b and c." i know this isn't possible, so i was wondering if my best solution is to have "a create instances of b and c."

i really think of b and c as modules. i need a (the application class) to use b and c (other classes like error reporting or user auth). how would you go about doing this?

thanks
Copy linkTweet thisAlerts:
@NogDogSep 19.2006 — Generally, I would reserve extending of classes for creating more specific cases of the extended class, e.g.:
[code=php]
class NationalLeagueTeam extends BaseballTeam extends SportsTeam
[/code]

If all your above classes need to use your Database class, for example, then I'd probably instantiate it in the SportsTeam class:
[code=php]
<?php
require_once "Database.class.php";
class SportsTeam
{
var $db;
function SportsTeam()
{
$this->db = new Database();
}
}
?>
[/code]

Caveat: I'm not claiming this is accepted "best practice" in the object-oriented world, it just make more sense to me, personally.
Copy linkTweet thisAlerts:
@JDM71488authorSep 19.2006 — Thank you both. ?
Copy linkTweet thisAlerts:
@BuezaWebDevSep 19.2006 — Make an interface? Interfaces are supported in PHP5.
Copy linkTweet thisAlerts:
@JDM71488authorSep 20.2006 — I have sorted out what I want to do and have come up with 3 options, excluding interfaces.

based on using the classes (error, db, login, app) with the app class being whatever app i am creating, consider the following.

[b]option 1: [/b]error extends db extends login extends app

this way the app object i create accesses all of the functions and since all of the member functions and variables are in app everything can access everything.

however, i agree with what nogdog said about extending related classes that build upon each other.

[b]option 2: [/b]create objects of each class inside of app.

this will require me to create the same functions over in app so that the app object i create can access the functions and variables of the classes it contains. otherwise, i cannot directly access the functions and variables of the other classes through my app object.

[b]option 3: [/b]create objects of each class on the page.

this is good in that i can access the functions and variables of each class directly through the objects i create. however, i will have to do a lot of logic programming that ties the classes together on the page. i can do this, but i'd rather have everything handled in the class.

i don't see the interface working because the classes must implement the interface, so i'd have to create a db class and have it implement every app i create.
×

Success!

Help @JDM71488 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 5.2,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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