I'm creating a CRUD page. My first approach was use the same class editCategory.php for doing these actions:If this file is called via GET and categoryId parameter doesn't exist -> shall show a empty formIf this file is called via GET and categoryId parameter is provided -> shall show a form with the data of this category.If this file is called via POST and there is no categoryId parameter -> shall create a new category.If this file is called via POST and categoryId parameter is provided -> shall update this category.The code works ok, but I have the sense that the code is cluttered and that it could be organized better.     <?php        require_once ../include_dao.php;        $action = isset($_POST[action]);        $category = new Category();        $categoryDao = new CategoryMySqlDAO();        $categoryName = ;        if ($action == save) {                                        // DO_POST            $categoryName = $_POST[name];            $category->name=$categoryName;            if (isset($_POST[categoryId])) {                              // update                $categoryId = (int) $_POST[categoryId];                $category->categoryId=$categoryId;                $categoryDao->update($category);            } else {                                                        // insert                $categoryDao->insert($category);            }            $messageSuccess = Category saved;        } else {                                                        // DO_GET            if (isset($_GET[categoryId])) {                $categoryId = (int) $_GET[categoryId];                $category = $categoryDao->load($categoryId);                $categoryName = $category->name;            }         ?><html> HTML code here showing the form</html>
Insert, update and get in the same php file?
php
I am not a php developer, but I think that your objects should encapsulate a more logic, I made a small simple example:$category = new Category();$categoryDao = new CategoryMySqlDAO();if ($_SERVER['REQUEST_METHOD'] === 'POST') {        if (isset($_POST[action])        {            //$category has logic with get varialbles in method fill             $category->fill($_POST);            //$categoryDao has logic insert or update $category            $result = $categoryDao->process($category);            //.. do something with $result        }} else if (isset($_GET[categoryId])){    $categoryId = (int) $_GET[categoryId];    $category = $categoryDao->load($categoryId);}