dongzhuo1930 2017-04-08 07:23
浏览 25

PHP OOP上传表单条目

IM trying to move away from procedural code and starting to get my feet wet with OOP, it is slow going but I am learning. I would like some help / opinion on the following:

Consider The Following View / HTML page which creates a simple form

enter image description here

An extract of the Class CreateJob{} I created looks like this:

function uploadJob($category, $headline, $description, $location){

        $sql ="INSERT INTO jobs (category, headline, description, location)
                        VALUES(:category, :headline, :description, :location)";
        $stmnt = $db->prepare($sql);
        $stmnt->bindValue(':category', $category);
        $stmnt->bindValue(':headline', $headline);
        $stmnt->bindValue(':description', $description);
        $stmnt->bindValue(':location', $location);
        $stmnt->execute();
        if($stmnt->rowCount() > 0){
            return $success = "Job Successfully Created";
        }
        else{
            return $error = 'SOMETHING WENT WRONG';
        }
    }

When the form is submitted the following is triggered via isset() and the object $newJob is created

if(isset($_POST['postJob'])){

         $obj = new CreateJob();

        $jobCategory = $_POST['jobCategory'];
        $jobDescription = $_POST['jobDescription'];
        $jobHeadline = $_POST['jobHeadline'];
        $jobLocation = $_POST['jobLocation'];

       $newJob = $obj->uploadJob($jobCategory, $jobDescription, $jobHeadline, $jobLocation);
      }

Everything works fine, however this code feels to procedural for me, and I feel I am missing some OOP concepts...?

Specifically is my method uploadJob() inside the CreateJob{} class correct or should I change some aspects of it?

Any other help / advice appreciate. Please keep in mind im a rookie!

EDIT:

Here is part of my table jobs, should anybody want to view it when attempting to answer the question

enter image description here

  • 写回答

1条回答 默认 最新

  • dongpiao1983 2017-04-08 08:46
    关注

    OOP is most about organization n classes on the system so it's hard to judgement about oop sinking about one class but here can be some improvement

    Trying to use factory pattern to create new objects. In client code it can be looks like

     $obj = jobFactory::create(some params)
    

    better pass values to uploadJob as array of parameters or even better some object that get $_POST array and convert it in proper object for uploadJob method

    also you can use try catch for example //in controller

     try {
          $obj = jobFactory::create(some params);
       //but it's question why you need new object here?
          $obj->uploadJob($obj with post parameters)
        }
        catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "
    ";
        } 
    

    and somewhere in uploadJob or jobFactory::create can trow error

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真