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;
}
}
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.
发布评论
评论(2)
抱歉,这还不是正确的方法。静态函数是包装在类中的过程代码。去掉所有静态,它看起来会更像 OOP,尽管 OOP 实际上是关于您创建的对象的交互。一个接一个地调用一个方法来实现你想要的只是一种编写函数的奇特方式。
这就是我要做的:
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:
是的,你的方法是正确的。遵循一个 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.