搜索 OOP - 模型设计 (MVC)

发布于 2024-12-09 17:24:25 字数 1432 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光的暖冬 2024-12-16 17:24:25

抱歉,这还不是正确的方法。静态函数是包装在类中的过程代码。去掉所有静态,它看起来会更像 OOP,尽管 OOP 实际上是关于您创建的对象的交互。一个接一个地调用一个方法来实现你想要的只是一种编写函数的奇特方式。

这就是我要做的:

class modelSearch extends Model {

   /**** public variables should not be common in OOP.  We want to control
    **** the interface.  Someone could change these if they were left public.
    ****/
   private $id;
   private $shopName;
   private $isOpen = false;

   public function __construct($shopName)
   {
      $this->shopName = $shopName;
   }

    public function findAll($searchType) {
        $SQL = "SELECT * FROM Shop as S
                 JOIN shop_options as O on O.ShopID = S.ShopID
                 JOIN shop_openhours as OH on OH.ShopID = S.ShopID
               WHERE (O.postcode = :postcode  OR S.company LIKE :companyName OR S.town = :townName)";


        /**** There is no need to instantiate any more objects in the class to
     **** do the work. I am replacing the rest of your function with
     **** pseudo-code.
     ****/
    $data = execution_of_sql();

    // I am not clear on what you want.
        foreach ($data as $record) {
       if ($this->isShopOpen($record['opentime'], $record['closetime']))
       {
          $openShops[] = array('id' => $record['id'],
                   'name' => $record['shopName']);
       }
    }

    return $openShops;

    // **** Notice how there is no need to instantiate another object
    // **** because we are the object.
    // $searchModel = new modelSearch();

    }

    // **** These could possibly be joined into 1 function with more parameters.
    public function findByTown($search) {
        //Same code as findAll() apart from SQL query (WHERE)
        //WHERE S.town = :townName
    }


    // **** protected and private hide information so that you can ignore it
    // **** outside the class.  The interface to the class is now less
    // **** cluttered.
    protected function isShopOpen($open, $close) {
        $min = ($open > 123) ? $open : 345; // Earliest allow time
        if ($close < $min)
        //some block code here
            return true;
        return false;
    }
}

Sorry, this is not the right way yet. Static functions are procedural code wrapped in a class. Take out all of the static and it will look a lot more like OOP, although OOP is really about the interaction of objects that you create. Calling one method after the other to achieve what you want is just a fancy way of writing functions.

Here is what I'd do:

class modelSearch extends Model {

   /**** public variables should not be common in OOP.  We want to control
    **** the interface.  Someone could change these if they were left public.
    ****/
   private $id;
   private $shopName;
   private $isOpen = false;

   public function __construct($shopName)
   {
      $this->shopName = $shopName;
   }

    public function findAll($searchType) {
        $SQL = "SELECT * FROM Shop as S
                 JOIN shop_options as O on O.ShopID = S.ShopID
                 JOIN shop_openhours as OH on OH.ShopID = S.ShopID
               WHERE (O.postcode = :postcode  OR S.company LIKE :companyName OR S.town = :townName)";


        /**** There is no need to instantiate any more objects in the class to
     **** do the work. I am replacing the rest of your function with
     **** pseudo-code.
     ****/
    $data = execution_of_sql();

    // I am not clear on what you want.
        foreach ($data as $record) {
       if ($this->isShopOpen($record['opentime'], $record['closetime']))
       {
          $openShops[] = array('id' => $record['id'],
                   'name' => $record['shopName']);
       }
    }

    return $openShops;

    // **** Notice how there is no need to instantiate another object
    // **** because we are the object.
    // $searchModel = new modelSearch();

    }

    // **** These could possibly be joined into 1 function with more parameters.
    public function findByTown($search) {
        //Same code as findAll() apart from SQL query (WHERE)
        //WHERE S.town = :townName
    }


    // **** protected and private hide information so that you can ignore it
    // **** outside the class.  The interface to the class is now less
    // **** cluttered.
    protected function isShopOpen($open, $close) {
        $min = ($open > 123) ? $open : 345; // Earliest allow time
        if ($close < $min)
        //some block code here
            return true;
        return false;
    }
}
不必你懂 2024-12-16 17:24:25

是的,你的方法是正确的。遵循一个 MVC 框架。我认为你正在遵循一个框架。你创建了一个类搜索作为模型,并且还有控制器和视图。

Yes,You are in the right way.Follow one MVC framework.I think you are following one frame work for this.You are created one class Search as Model and also have controller and view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文