0

It's a little difficult to explain.

Suppose you are creating a CMS. In that CMS you want user to be able to add pages, edit them and delete them. You also want user to be able to add posts, edit them and delete them. You also want user to be able to add, edit, delete products and add, edit, delete categories... (you get the idea.)

Now the way i would handle this problem is i would create separate forms and submit them on the same file that would handle all the process of adding, editing, deleting...

For example: i would create a addpage.php. It'll have a form and it'll submit to a process.php file. The same with posts. addpost.php file and submit to process.php. In these forms I'll also send a hidden input named "p" to identify the process. For example: on addpage.php the hidden input will be <input type="hidden" name="p" value="addpage"> on addpost.php the hidden input will be <input type="hidden" name="p" value="addpost">. So in the process.php i will just check the $_POST['p'] using ifelse statements and just perform the task. Some thing like:

if( $_POST['p'] == "addpage" ){ // the process for adding page }elseif( $_POST['p'] == "editpage" ){ // the process for editing a page }elseif( $_POST['p'] == "addpost" ){ // the process for adding a post }

Now this method obviously works fine, but this does not feels like a good programming technique..

How can i handle this in a better way? How can I this OO-ly.

I'm new to OOP and I know most of you will tell me to use a framework. But this is mostly for learning. What is the best possible way of handling this without MVC?

Ameer
  • 761
  • 1
  • 6
  • 19

2 Answers2

1

Consider this example according to your $_POST['p']

// my_request_handler.php
class Handle_Page{

    public $Requested_Page;

    function __construct($request_for) {
        // Holds $_POST['p']
        $this->Requested_Page = $request_for;
    }

    function handle_request() {
        switch( $this->Requested_Page ) {
            case "addpage":
                // call add_page method
                $this->add_page();
            break;
            case "editpage":
                ..............
                ..............
            break;
            case "deletepage":
                ..............
            break;
        }

        function add_page() {
          /// Define functionality here
        }
    }

}

Now come to your code:

if( $_POST['p'] == "addpage" ) { 
    // the process for adding page 
} 
elseif( $_POST['p'] == "editpage" ) { 
   // the process for editing a page 
}
elseif( $_POST['p'] == "addpost" ) { 
  // the process for adding a post 
}

this part do like this:

include 'my_request_handler.php';
$Request_Handler = new Handle_Page($_POST['p']);
$Request_Handler->handle_request();

Now its work same like your have done without oops. According to this example i separated the request handling for the pages in Handle_Page class and return the response wherever you want. You just have to include this class.

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
  • But its the same. isn't it? Is this the best I'm gonna get without MVC? – Ameer Mar 22 '14 at 08:59
  • @Ameer what do you mean by MVC? – jogesh_pi Mar 22 '14 at 09:00
  • the frameworks... codeigniter, zend... – Ameer Mar 22 '14 at 09:02
  • @Ameer they are not MVC, they just follow the MVC Architecture. Take a look on http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller and http://stackoverflow.com/questions/26685/what-is-mvc-and-what-are-the-advantages-of-it#answer-26693 – jogesh_pi Mar 22 '14 at 09:04
  • Yes i know that's what i meant. – Ameer Mar 22 '14 at 09:05
  • yes it is similar to your, but with OOPS, and Separated. You can make it more robust as you can. You can implement Design patterns as well, but first you have to start with simple then into depth. Which is a good way to learning. – jogesh_pi Mar 22 '14 at 09:08
0

I would recommend you to 1. check out first how to write classes and methods in PHP. 2. First class you should target is database handling which should be doing the tasks such as connect to db, execute a SQL etc. 3. Then write a separate class which will use this db class to add, edit, update or delete the data for each entity such as pages, post, category etc. BTW go ahead first and try create some class. php.net or Google will also help a lot.

  • the is question about handling the the process not performing the action. I know can have something like: $page = new page(); $page->name = "Page Name"; $page->content = "Page Content"; $page->update(); ; – Ameer Mar 22 '14 at 09:18
  • My 3rd point says about it I think separate class for each entity would be better approach. At least this is how we go for it. It is always better to keep them separate as while fixing issue with one entity will not affect other. I think when you have every thing happening in one php script it will make it big script as well as difficult to maintain. – Niranjan Nimbalkar Mar 22 '14 at 13:31