Jump to content

What to do?


owner

Recommended Posts

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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, ..)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

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.