Search code examples
phpcode-structure

PHP application structure


I started making a website, and quickly i found out that my code is a mess. It's been a while since I've been programming in PHP, and since then I learned OOP. Now while making an application in C# or java is pretty easy from this point of view, PHP is giving me trouble.

I used to program everything (in php) just from one line to another, with minimum amount of methods and no classes at all. So how much should be in methods, and where should this classes be?

Example: I have account.php, and i want to update user information. So after checking that someone has sent some data if(isset($_POST[.. what next? I used to just make all the checks like $email=CheckForRisksOrSomething($_POST["email]); and then just update mysql.

This brings me to the question, should I create a class User and what methods should it contain? Also WHERE should this class be saved, assuming that user.php would show you user's profile.

Back to how much should be in classes, should i just use in a file like:

$smth = new Someclass();
if($smth->checkIfSaved()){
    $user = new User();
    $user->updateUser(/* all the $_POST stuff here? */); 
} else {
    $smth->showUserDetailsForm();
}

Also where should Someclass be saved?

So if ayone would give me an example of how to structure the following functionalities (in files, classes and possibly methods).

  • users (registration, login, editing account..)
  • picture with comments of it
  • news (also with comments)
  • administration

This is just an example of course, but i don't want to keep on with my mess and then look at it three days later and just facepalm and wonder what did i write there. Well, i already did that today, that's why I'm asking.


EDIT 24.2.2016

While this is an old question, as suggested look into a framework. While it might take a bit to set it up it increases development speed, code is nice and clean and a lot safer. I personally use Laravel.


Solution

  • There's more than one way to skin this cat, but I'll recommend some helpful links and books for you.

    Zend's Coding Standards: If you're looking at how to structure, name, etc. your files, then I would look at how Zend suggests to do these things. By following their guidelines, you can create a package that is compatible with somebody else's code using these same methods, as well.

    What you will want to do is create a folder specifically for your classes. How do you know when you'll need to make a new class, and what to include? Good question!

    If you find yourself running into groups of information that is related, needs to be handled together a lot, or solves a particular problem, chances are you should create a new class that can handle it.

    For example, a User class to handle any sort of changes you want to make to a user of your site. Another example is a Role class that can perform tasks (or not) depending on if they're an admin or not. The wonderful thing about Objects and Classes is you can create a User::$role (variable named $role in the User class) that is a Role class. So for instance, you will have a Role object in your User object. From here, your user object can now call to the role object by doing something like:

    class User {
        private $username;
        private $role;
    
        function __construct($username){
            $this->username = $username;
        }
        public function setRole($roleType){
            switch($roleType){
                case "admin":
                    $this->role = new Admin(); // contained in another class file
                    break;
                case "staff":
                    $this->role = new Staff(); // contained in another class file
                    break;
                default:
                    $this->role = new Visitor(); // contained in another class file
                } // end case
            } // end setRole
    } // end Class User
    
    $loggedInUser = new User("Bubba");
    $loggedInUser->setRole("admin");
    $loggedInUser->role->deleteMessage();
    

    In general, you will want to keep your code structured and separate, so that each class only handles information that it should have, and not do anything more than it should. If you find a class doing too many things, this means you found a good time to create another class to handle this new responsibility. How you tie it all together is generally a problem answered with "design patterns". There is a very good book that covers both programming using objects and classes, as well as using design patterns.

    Check out the book "PHP 5 Objects, Patterns, and Practice" by Matt Zandstra, and published by Apress. It's a wonderful book that dips your toes into OOP with PHP!