seddonym Posted March 26, 2007 Share Posted March 26, 2007 Hi I'm trying to convert a rather messy function based site into a cleaner object-oriented version (but I'm new to OOP). My basic strategy was to have a 'page' class that I would then instantiate on each page, for example:- friends.php <?php require_once('functions/mainfunctions.php'); $currentPage = new page; $currentPage->title = 'Friends Page'; $currentPage->membersOnly = true; $currentPage->begin(); $currentPage->displayFriendsList(); $currentPage->displayMenu(); $currentPage->displayFooter(); ?> As you can see there are certain properties I would set at the beginning of the page. I then call begin() which deals with access (linking to mysql database) and outputs the page headers. There are page-specific display methods next, and finally a method which displays the footers. The methods shown in the page all call lots of other methods so everything is very modular. My original intention was to create a lot of methods all within the page class definition (access methods, display methods, error handling methods etc.). I did this because I wanted them all to have access to the page's properties (which include things like the current User) via $this->[propertyname]. I wanted to put them into separate files and then use include() to put them into the one large class - but I now discover that isn't possible (and probably making one huge class is bad programming practice anyway). I could just have all the code in one file but that's very difficult to edit and strikes me as the wrong solution. Really what I need is a simple pointer on a good way of structuring an OOP site so that everything is kept very modular, (and in different files) but all the properties of the page are available at all times. Should I be using several classes, scope resolution operators, global variables, subclasses... Perhaps there is a good tutorial on this but I haven't been able to find one that specifically gives a simple overview of a site. Thanks enormously! Link to comment https://forums.phpfreaks.com/topic/44339-designing-a-basic-oop-site-framework/ Share on other sites More sharing options...
genericnumber1 Posted March 26, 2007 Share Posted March 26, 2007 The best way of making a site IMHO would be to use the Model-View-Controller pattern. It allows for some of the best modularity and separation of presentation and logic possible. http://www.phpfreaks.com/forums/index.php/topic,107835.0.html Look under the section "MVC" it has some good information on this. Link to comment https://forums.phpfreaks.com/topic/44339-designing-a-basic-oop-site-framework/#findComment-215659 Share on other sites More sharing options...
c4onastick Posted March 26, 2007 Share Posted March 26, 2007 I agree. But if you've got a pretty small site (or really simple layout and purpose) the way you described works perfectly. I've got an ugly little site that I use for personal stuff (bookmarks, price quotes, etc) and I just wrote a quick little "page" class like what you've got there. Works great, but genericnumber1 is right, not as modular as MVC. Link to comment https://forums.phpfreaks.com/topic/44339-designing-a-basic-oop-site-framework/#findComment-215678 Share on other sites More sharing options...
seddonym Posted March 28, 2007 Author Share Posted March 28, 2007 Thanks for your suggestions - I've had a look at the MVC pattern but it all seemed a bit too complex for what I want to do, at least for now. Perhaps something to learn for later, although a pity I wasn't able to find a really straightforward introduction to setting up a bare-bones MVC site. What I have ended up doing was implementing a rather half-hearted attempt at OOP. I have two classes, currentPage and user. I also have a subclass of user called currentUser, which I have implemented as a 'singleton'. This means I can easily access the currentUser properties globally using, for example: currentUser::singleton()->loggedIn or currentUser::singleton()->userName Everything else is procedural. On second thoughts, however, I can't really see the advantage of using these classes intermittently and will probably end up going back to a fully procedural model, using the user and page properties as global variables. If only there were a way of using include within class definitions so you could split the code up into different files, for ease of editing... Link to comment https://forums.phpfreaks.com/topic/44339-designing-a-basic-oop-site-framework/#findComment-216777 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.