linus72982 Posted July 24, 2010 Share Posted July 24, 2010 I have a design setup created for my site background scripting functions that "hubs". I'm moderately new to OOP so I'm not even sure if this is good design, but essentially I have a $hub object instantiated and the constructor for that class instantiates objects for each of my other classes so that I can then add one line at the top of every page that creates $hub as a new Hub and then call any function in any of my files through it (for example, to call sanitizeSession from my session class, I'd call $hub->session->sanitizeSession and then later I can call getUserInfo from the login class with $hub->login->getUserInfo, you get the picture.) Yes, I know I'm trying to make OOP into the old goto procedural languages by my design, but it's comfortable for me to start with, I'll get more advanced as I learn, I'm sure, but here's my question. Say, with this setup, I have called sanitizeSession from session using $hub->session->sanitizeSession - within the sanitizeSession function, how would I call "back" to another hub function, say $hub->login->functionIwant? Do I have to pass $this to each of the instances I create at the construct of hub and use that object to call back? I'm sure I can't use $this as it would actually call the object I'm in and not the "referring object". Can I maybe make all the spokes of hub extensions of hub and call back that way? How do I accomplish that? Thanks for any help and thanks for understanding my noobishness Link to comment https://forums.phpfreaks.com/topic/208742-calling-an-object-in-a-referring-object/ Share on other sites More sharing options...
trq Posted July 24, 2010 Share Posted July 24, 2010 I'm sorry but this absolutely wreaks of poor design and isn't what I would call OOP at all. Hence, you are at this problem. Why not design well in the first place using best practices and design patterns? Link to comment https://forums.phpfreaks.com/topic/208742-calling-an-object-in-a-referring-object/#findComment-1090538 Share on other sites More sharing options...
linus72982 Posted July 24, 2010 Author Share Posted July 24, 2010 I guess it wasn't so much that I didn't understand that it was bad design and why, it was more than I received a few conflicting "rules" from advice here. On the one hand I was told that you should keep object instantiations off your content pages, that it's supposed to happen in background scripts, okay, got it. So I started doing that, but then I got told that instantiating objects after the class close bracket was also bad practice (even though I've seen it in more scripts than I can count). Okay, so now I have to instantiate objects in the classes, as I use them and pass them as needed for use elsewhere, but then it got super complex trying to juggle all these objects floating out there after 9 or 10 function calls down the line, going back and trying to figure if I had already instantiated one of those and if I had passed it properly through the chain for use where I sit, oy. Can someone point me to a good tutorial that teaches how to design these sorts of website programs? If you need a reference to where I'm working from now, this is my design: Hub -> - sessionIO (so I don't have to call session_start on every page, just instantiate it and run all session actions through it) - login (processes login functions, new user functions, merging accounts from facebook/twitter/etc, etc) - dbInterface (opens the connection and contains all database input/output functions - processInput (processes and traffic cops all form submissions) - validation (validates input, whether it be checking length, preg_matching, escaping, etc, usually called from processInput) - errorHandle (handles errors, adds to the error array, organizes multiple errors, traffic cops where to go for certain level errors, etc) - config (definitions for things I can reasonably assume will change from time to time such as DB_SERVER, DB_PASSWORD, options I can turn on and off, etc) So all of those are instantiated by Hub, and hub is instantiated by each page, and I call functions through it. Am I even anywhere close to the right path or should I scrub everything and start over? Any pointers on how? Thanks for any assistance. Link to comment https://forums.phpfreaks.com/topic/208742-calling-an-object-in-a-referring-object/#findComment-1090542 Share on other sites More sharing options...
trq Posted July 24, 2010 Share Posted July 24, 2010 The best advice I could give to learn OOP is to use one of the already existing OOP frameworks. Even if you end up rollong your own (as I did) a well designed framework will teach you allot. Zend was where I started but it has a pretty steep learning curve. There's plenty of others around though. Link to comment https://forums.phpfreaks.com/topic/208742-calling-an-object-in-a-referring-object/#findComment-1090544 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.