deadonarrival Posted October 4, 2007 Share Posted October 4, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71874-meep-using-classes-within-classes-and-so-on/ Share on other sites More sharing options...
deadonarrival Posted October 4, 2007 Author Share Posted October 4, 2007 And... (Can't find an "edit button" :S) Would it make sense if some of my classes were turned into functions? There are some where there's only one function in the class... might make it easier to put them as a normal function? Or group them into a "functions" class? Quote Link to comment https://forums.phpfreaks.com/topic/71874-meep-using-classes-within-classes-and-so-on/#findComment-362075 Share on other sites More sharing options...
trq Posted October 5, 2007 Share Posted October 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71874-meep-using-classes-within-classes-and-so-on/#findComment-362097 Share on other sites More sharing options...
deadonarrival Posted October 5, 2007 Author Share Posted October 5, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/71874-meep-using-classes-within-classes-and-so-on/#findComment-362531 Share on other sites More sharing options...
emehrkay Posted October 5, 2007 Share Posted October 5, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71874-meep-using-classes-within-classes-and-so-on/#findComment-362540 Share on other sites More sharing options...
Cobby Posted October 6, 2007 Share Posted October 6, 2007 I suggest you read this MVC tutorial: HTML: http://www.phpit.net/article/simple-mvc-php5/ PDF: http://www.phpit.net/article/simple-mvc-php5/?pdf=yes Quote Link to comment https://forums.phpfreaks.com/topic/71874-meep-using-classes-within-classes-and-so-on/#findComment-363027 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.