owner Posted April 13, 2010 Share Posted April 13, 2010 Hello, I am working on my own little project but it seems to be getting consistently messier. Right now, I have one file with a few classes in it. Each class is its own thing. database class for database, sessions for sessions, etc. My main class that is executed consists of a switch that gets what page the user is on and then calls the appropriate function in my main file. For example here is the layout of my script. file.php $page = new page(); echo $page->runApp(); class run{ function construct(){ } function runApp(){ switch($event){ case 'doSomething': $this->doSomething(); break; default: $this->doSomething(); break; } function doSomething(){ //Code } } } class database{ //Stuff } class session{ //stuff } So having one big file with everything in it is messy and a giant bloated file. I would like to move to something that is a bit more organized/logical and could be expanded easier. Could someone please point me in a direction to go? Thanks in advance, owner Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/ Share on other sites More sharing options...
Jax2 Posted April 13, 2010 Share Posted April 13, 2010 Don't know if this is relevant, or even helpful, but I find that when my pages get massive, I break them down into smaller chunks and use include("") to include different parts. My recipe script, for example, the page that shows the recipe also includes where people can upload their own, saving it, viewing them by different methods ...etc, but the page itself which would normally be 800+ lines is down to about 20 lines by just including all the separate actions in their own php file. Again, don't know if that's what you're getting at or not, just thought I'd share. Sorry if irrelevant. Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040691 Share on other sites More sharing options...
owner Posted April 13, 2010 Author Share Posted April 13, 2010 I think I am looking for something more automatized? If I recall, I have seen some scripts online where you can store each page in its own file (class?) and based on the name of the file/class it is auto loaded. I would like to make this faster/easier to add/remove modules from my site. Any ideas are appreciated owner Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040695 Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 So you're looking for some kind of registry class? You can just have an array to keep track of the modules etc and load them as need be. Of course, tied to the DB so you can turn it on and off. Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040699 Share on other sites More sharing options...
owner Posted April 13, 2010 Author Share Posted April 13, 2010 I am not concerned with the database side of this to turn on/off things. I am concerned about how to make my application logical. I want each section of the site in its own class rather than each section of my site in one huge file. So I need some sort of way to figure out what file needs to be pulled to bring up that information rather than one file that pulls up everything. Basically, I need some sort of controller that controls what needs to be loaded and how it loads. I know I do not make much sense but I am a bit lost too lol owner Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040702 Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 No, it makes sense. Create a class/interface and have other classes work with it. Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040705 Share on other sites More sharing options...
owner Posted April 13, 2010 Author Share Posted April 13, 2010 Do you have any examples? That is what I am stuck on Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040715 Share on other sites More sharing options...
andrewgauger Posted April 13, 2010 Share Posted April 13, 2010 I tend to break my applications up into individual classes. Lets say I want to have an object instanciation class that loads from a database, and on deconstruction, if anything changed, updates the database (this is the fundamental use of php for me--creating objects that do the database interaction) So back to that class. Lets say I am creating user objects. Now, I know I want to use user objects in A LOT of code, so I want to make it easy for me to use this class again next time I build something. I create a class.user.php file. class.user.php include_once("connect.php"); class User { private $id; private $name; private $email; private $modified; public function __construct($id) { $query="SELECT id, name, email FROM user WHERE id='$id' LIMIT 1"; //FETCH ASSOC, set $this->id, $this->name, etc; } public function __destruct() { //if $this->modified{update code} } public function getID($id) { if(isset($id)){ $this->id=$id; $this->modified=1; } return $this->id; } I'm not going to bore you with an entire oop representation, but that is all that goes in the file. Next time I want to use my user class, I just throw an "include_once("class.user.php"); and use it. You will wish you did this the very next time you build a program, I guarantee it. Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040720 Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 Pardon for asking, but what's the purpose of the getID method? If it just returns the value given to it, it's not all that useful. Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040725 Share on other sites More sharing options...
trq Posted April 13, 2010 Share Posted April 13, 2010 I think I am looking for something more automatized? If I recall, I have seen some scripts online where you can store each page in its own file (class?) and based on the name of the file/class it is auto loaded. It sounds to me like your looking for a very simple MVC implementation. I had written up a very simple example for a user here probably 2 years ago but the search isn't working at the moment so I can't locate it. Maybe if you search Google for a 'simple php mvc' you will find something. Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040726 Share on other sites More sharing options...
andrewgauger Posted April 13, 2010 Share Posted April 13, 2010 Pardon for asking, but what's the purpose of the getID method? If it just returns the value given to it, it's not all that useful. the properties of the class are private. modifying a value needs to set modified=1 when deconstructing if modified=1 then update, if not, dont waste the clicks Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040736 Share on other sites More sharing options...
ignace Posted April 13, 2010 Share Posted April 13, 2010 class ClassLoader { public function __construct() { spl_autoload_register(array($this, '__autoload')); } public function __autoload($class) { //logic } } class MyApplication { private $autoloader; public function __construct() { $this->autoloader = new ClassLoader(); } public function bootstrap() { //logic $this->registerParam('MyApplication', $this); $this->boot($this->getParams()); } } MyApplication wraps the entire application much like Zend_Application does, the advantage is that your otherwise global variables are now wrapped in an object which are accessible throughout your entire application and accessible through some mechanism in the Zend framework you would access the application object by using: $this->_getParam('bootstrap')->getApplication(); However to design your application in such a way will require a good deal of design pattern knowledge (GoF, Martin Fowler, Craig Larman, Eric Evans, ..) Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040781 Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 the properties of the class are private. modifying a value needs to set modified=1 when deconstructing if modified=1 then update, if not, dont waste the clicks I think you meant set instead of get. It doesn't exactly *get* anything. Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040949 Share on other sites More sharing options...
andrewgauger Posted April 13, 2010 Share Posted April 13, 2010 The function combines the get and set attribute, so you don't have to call a different function to set a property. I could call it getset, but that just sounds funny. Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040962 Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 The function combines the get and set attribute, so you don't have to call a different function to set a property. I could call it getset, but that just sounds funny. No, because it doesn't make any sense. If you were to write such a function, you should make the $id parameter optional. Otherwise, you're always getting back the value you pass in. Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1040966 Share on other sites More sharing options...
andrewgauger Posted April 13, 2010 Share Posted April 13, 2010 Your right, I forgot to set the default value to =NULL as it was supposed to be optional. I guess I flew through the sample too quickly. Also, I just found out isset doesn't work with =NULL, so I should have put public function getID($id=NULL) { if($id){ $this->id=$id; $this->modified=1; } return $this->id; } But I also, shouldn't have used this example, since the ID isn't modifiable, this sample would have made more sense if I had shown setting/getting the name value instead. Typed this up last thing before bed, and it would have been better if I hadn't even bothered posting it. But the point I was trying to make is to put the class into its own file. Quote Link to comment https://forums.phpfreaks.com/topic/198340-what-to-do/#findComment-1041112 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.