Jump to content

Meep, using classes within classes and so on.


deadonarrival

Recommended Posts

Hai.

 

I'll get straight to my point, cuz its late and I'm sleepy and confused. :)

 

Basically I have a series of classes... but no idea how I'm supposed to link them.

 

I have, for example...

var.class (a couple of functions to clean user/database input)

db.class (database connection, needed for everything)

content.class (used to generate the DIVs for the content boxes (not the layout))

cms.class (used to choose which content to show)

 

And various other classes that run the actual content (eg news, blogs, calender, gallery etc)

 

Right now I have the classes in a heirachical order, where a class extends anything it uses.

eg class DB extends VAR

class CONTENT extends DB

class CMS extends CONTENT

 

and so on....

I'm sure this is the wrong way to go about it, and causes problems when I try to remember my class structure. I almost think it would be easier to go back to procedural coding and include files where I need them, but that would defeat the object.

 

Does anyone have any advice/suggestions on how to link classes (in any situation) so that they work together more efficiently? Can I just have a list of classes, and each one extends all the others?

 

Is there any way to make it so that any class can use any function from any of the other classes I have? <- My preferred solution

 

Thanks

Link to comment
Share on other sites

I'm no oop expert by a long stretch but IMO a class shouldn't extend another class unless it adds functionality to the parent. The way your doing it at the moment your extending classes to add functionality to the child. Why would content extend db? It adds no related functionality to db. The only reason you have done it that way is because you need to use the db classes methods within content.

 

For instance, will you ever call one of the db classes methods via an instance of content? eg;

 

<?php

  $content = new content();
  $result = $content->query("SELECT * FROM foo");

?>

 

Probably not, and why? Because it doesn't make sense, it doesn't even look right. You can do it though the way your classes are designed.

Link to comment
Share on other sites

So what's the best way for me to be able to use the DB class functionality from within the content class? Unless I declare the new object in each class?

 

Should I just create the $db = new db(); in each class I want to be able to use it?

 

yes. look into the singleton so that you dont have to keep reconnecting to the db everytime a different script needs to use it

 

http://en.wikipedia.org/wiki/Singleton_pattern#PHP_5

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.