PHP application structure

I started building a website, and quickly found out that my code was a mess. Some time has passed since I programmed in PHP, and since then I have learned OOP. Now that a C # or java application is pretty easy from this point of view, PHP is giving me problems.

I used for programming everything (in php) from only one line to another, with a minimal number of methods and without classes at all. So, how much should be in methods and where should these classes be?

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

This brings me to the question: create a User class and what methods should it contain? Also WHERE should save this class, assuming user.php will show you the user profile.

Let's get back to how much class should be, I should just use in the file, for example:

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

Should you also keep Someclass?

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

  • users (registration, login, registration) ..
  • image with comments
  • news (also with comments)
  • Administration

This, of course, is an example, but I do not want to continue my mess, and then I will look at it three days later and just look at what I wrote there. Well, I already did it today, which is why I ask.


EDIT 24.2.2016

Although this is an old question, it was supposed to take a look at the structure. Although it may take a little to configure it, it increases the development speed, the code is nice and clean, and much safer. I personally use Laravel.

+4
source share
4 answers

There is more than one way to trick this cat, but I will recommend you useful links and books.

Zend coding standards: If you look at the structure, name, etc. your files, I would see how Zend suggests doing this. Following their recommendations, you can create a package that is compatible with other people's code using the same methods.

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

If you find that you work with groups of related information, you need to process a lot of them, or solve a specific problem, most likely you should create a new class that can handle it.

For example, the User class handles any changes you want to make to the user of your site. Another example is a role class that can perform tasks (or not) depending on whether they are an administrator or not. The great thing about objects and classes is that you can create the user role :: $ (a variable named $ role in the User class), which is the Role class. So, for example, you will have a Role object in a User object. Now your custom object can now call 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 processes only the information that it should have, and do nothing more than necessary. If you find that a class does too many things, it means that you have found the right time to create another class to handle this new responsibility. As you tie it all together, usually the problem is with the “design patterns”. There is a very good book that covers programming using objects and classes, as well as the use of design patterns.

Check out Matt Zandstra's book PHP 5 Objects, Patterns and Practice and published by Apress. This is a great book that dips your fingers in OOP with PHP!

+3
source

It is very difficult to tell you how you should do this. There are many different preferences. Look at frameworks like Zend , Kohana , CodeIgniter , etc.

When I create small applications with OOP design, I usually try to follow the following hierarchy:

  • In index.php include bootstrap.php .
  • All classes and config are included in bootstrap.php .
  • Create a classes directory in which you can save the basic functionality in the core folder.
  • In addition, in your classes directory, create two controllers and models folders.
  • In my projects, I usually have another templates folder, which includes all my html files (and subsequently they are displayed using the Smarty template engine).

This is a very simplistic approach, but it can lead you in the right direction. For your requirements, such as users, images, etc., Create a model containing all the data of one user.

 class UserModel() { public $name; public $email; } 

And then a controller that can change this model:

 class UserController() { public function update($user) { ... } } 
+1
source

My answer may not directly answer your questions, but it can do you a lot of good.

Go with a real frame. Do not create sites with code from scratch. This will save you a huge amount of time and force you to use advanced code separation techniques, a really good and clean project structure, but again, it will save you time! For many things that you decide to implement there, there will be a ready-made solution, and the usual thing that you do (like confirming your email address) will be done correctly.

I would go with the MVC PHP framework and after using a lot of CakePHP, Zend, Kohana and some others in the past, I would recommend you go with Yii. A very small learning curve, nice documents, a nice big active community, and you can transfer everything that you already have into this very short time.

I am sure that the project that you described will reduce your time by at least 2 times, and at the same time you will have everything that you ask for above, which have already been resolved. I know this from experience, having long since developed projects from scratch.

0
source

If you are developing for the Internet, there is a good chance that you will be developing an MVC application, the simplest MVC framework that I know of is CodeIgniter. And I advise you to use it.

0
source

All Articles