如何使用PHP OOP创建选择列表

如何使用PHP OOP创建选择列表,php,class,selectlist,Php,Class,Selectlist,我已经开始将一个PHP项目重新编码到OOP中。有一件事我在很多人中都搞不懂,那就是如何创建一个动态选择列表。我有许多查找选择列表要制作。最好的办法是什么 我创建了一个DatabaseObject类,其中包含我所有的通用数据库查询。我是在这里添加它们还是为它们创建一个特殊的类,以及如何对其进行编码 require_once("database.php"); class DatabaseObject { protected static $table_name; // find all fro

我已经开始将一个PHP项目重新编码到OOP中。有一件事我在很多人中都搞不懂,那就是如何创建一个动态选择列表。我有许多查找选择列表要制作。最好的办法是什么

我创建了一个DatabaseObject类,其中包含我所有的通用数据库查询。我是在这里添加它们还是为它们创建一个特殊的类,以及如何对其进行编码

require_once("database.php");

class DatabaseObject {

protected static $table_name;

// find all from a specific table
public static function find_all(){      
    global $database;
    return static::find_by_sql("SELECT * FROM ".static::$table_name);       
}

// select all from a specific table
public static function find_all_from($table){       
    global $database;
    return static::find_by_sql("SELECT * FROM " .$table);       
}

// find all from a specific table
public static function find_by_id($id){

    global $database;
    $result_array = static::find_by_sql("
    SELECT * FROM ".static::$table_name. " WHERE id = '{$id}' LIMIT 1");

    // return the data only for the one user
    return !empty($result_array) ? array_shift($result_array) : false;      
}

// find using sql
public static function find_by_sql($sql=""){

    global $database;
    // return all data from sql
    $result_set = $database->query($sql);
    $object_array = array();
    while($row = $database->fetch_array($result_set)){
        $object_array[] = static::instantiate($row);    
    }
    return $object_array;       
}


protected static function instantiate($record){

    $class_name = get_called_class();
    $object = new $class_name;

    foreach($record as $attribute=>$value){
        if($object->has_attribute($attribute)){
            $object->$attribute = $value;
        }
    }
    return $object;
}


protected function has_attribute($attribute){

$object_vars =  $this->attributes();

// here we only want to know if the key exist
// so we will return true or false
return array_key_exists($attribute, $object_vars);      
}

protected function attributes() {

$attributes = array();
foreach(static::$db_fields as $field) {
    if(property_exists($this,$field)) {
    $attributes[$field]= $this->$field; 
    }
}
return $attributes; 
}

protected function sanitised_attributes() {
global $database;
$clean_attributes = array();
foreach($this->attributes() as $key => $value){
$clean_attributes[$key] = $database->escape_value($value);  
}
return $clean_attributes;   
}


public function save() {
// A new object won't have an id yet
return isset($this->id) ? $this->update() : $this->create();
}

// create new  
protected function create() {
global $database;
$attributes =$this->sanitised_attributes();

$sql  = "INSERT INTO ".static::$table_name." (";
$sql .= join(", " ,array_keys($attributes));
$sql .= ") VALUES ( '";
$sql .= join("', '" ,array_values($attributes));
$sql .= "')";
if($database->query($sql)) {
    $this->id = $database->insert_id();
    return true;
} else {
    return false;
}
}

// update details
protected function update() {
global $database;
$attributes =$this->sanitised_attributes();
$attribute_pairs = array();
foreach($attributes as $key => $value) {
    $attribute_pairs[] = "{$key}='{$value}'";   
}
$sql  = "UPDATE " .static::$table_name. " SET ";    
$sql .= join(", ",$attribute_pairs);
$sql .= " WHERE id=". $database->escape_value($this->id);
$database->query($sql);
return ($database->affected_rows() ==1) ? true : false ;
}


public function delete() {
global $database;

$sql  = "DELETE FROM ".static::$table_name;
$sql .= " WHERE id =". $database->escape_value($this->id);
$sql .= " LIMIT 1";
$database->query($sql);
return ($database->affected_rows() ==1) ? true : false ;
}
}

只需创建一个返回HTML选择/选项视图的方法,通过迭代传递给该方法的关联数组。。。?可能是这样的:

public static function viewSelect($name = "select", $arr_options = array()) {
    $html = "<select name='$name'>\n";
    foreach ($arr_options as $key => $val) {
        $html .= "<option value='$key'>$val</option>\n";
    }
    $html .= "</select>\n";
    return $html;
}
公共静态函数viewSelect($name=“select”,$arr\u options=array()){
$html=“\n”;
foreach($key=>$val的arr_选项){
$html.=“$val\n”;
}
$html.=“\n”;
返回$html;
}

然后将一个数据库查询的结果传递给此方法。您可以将此方法放入任何适当的类中。

只需创建一个方法,通过迭代传递给该方法的关联数组,返回HTML选择/选项视图。。。?可能是这样的:

public static function viewSelect($name = "select", $arr_options = array()) {
    $html = "<select name='$name'>\n";
    foreach ($arr_options as $key => $val) {
        $html .= "<option value='$key'>$val</option>\n";
    }
    $html .= "</select>\n";
    return $html;
}
公共静态函数viewSelect($name=“select”,$arr\u options=array()){
$html=“\n”;
foreach($key=>$val的arr_选项){
$html.=“$val\n”;
}
$html.=“\n”;
返回$html;
}

然后将一个数据库查询的结果传递给此方法。您可以将此方法放入任何适当的类中。

您还可以添加所选选项功能

公共静态函数viewSelect($name=“select”,$arr\u选项= 数组(),$selected){

$selectedhtml=”“;
$html=“\n”;
foreach($key=>$val的arr_选项){
如果($key==$selected)$selectedhtml=“selected”;
$html.=“$val\n”;
}
$html.=“\n”;
返回$html;}

您还可以添加所选选项功能

公共静态函数viewSelect($name=“select”,$arr\u选项= 数组(),$selected){

$selectedhtml=”“;
$html=“\n”;
foreach($key=>$val的arr_选项){
如果($key==$selected)$selectedhtml=“selected”;
$html.=“$val\n”;
}
$html.=“\n”;
返回$html;}

我肯定会将select列表建模为一个对象,因为它有一个可以封装的定义良好的职责。我会尽可能地使它与
数据库对象
分离,这样任何一个类中的更改都不会影响其他类。例如,请考虑:

class SelectList
{
    protected $options;
    protected $name;

    public function __construct($name, $options)
    {
         $this->options = $options;
         $this->name = $name;
    }

    public function render()
    {
        $html = "<select name='" . $this->name . "'>\n";
        foreach ($this->options as $option) 
        {
            $html .= $option->render();
        }
        $html .= "</select>\n";
        return $html;
    }
}

class SelectListOption
{
    protected $label;
    protected $value;
    protected $isSelected;

    public function __construct($label, $value, $isSelected = false)
    {
      //Assign the properties
    }

    public function render()
    {
        $html .= '<option value="' . $this->value . '"';
        if ($this->isSelected)
        {
         $html .= ' selected="selected" ';
        }
       $html .= '>' . $this->label . "</option>\n";
    }
}
class选择列表
{
受保护的美元期权;
受保护的$名称;
公共函数构造($name,$options)
{
$this->options=$options;
$this->name=$name;
}
公共职能
{
$html=“\n”;
foreach($this->options as$option)
{
$html.=$option->render();
}
$html.=“\n”;
返回$html;
}
}
类SelectListOption
{
受保护的$标签;
受保护的美元价值;
受保护的选举;
公共函数构造($label,$value,$isSelected=false)
{
//分配属性
}
公共职能
{
$html.=''.$this->label.\n;
}
}
我喜欢以这种方式对事物进行建模的一点是,添加新特性(例如,选定/未选定项的CSS样式,或禁用的属性)非常容易,因为您知道新特性属于哪个对象。此外,拥有这种“小”对象使编写单元测试变得非常容易


HTH

我肯定会将选择列表建模为一个对象,因为它有一个可以封装的定义良好的职责。我会尽可能地使它与
数据库对象
分离,这样任何一个类中的更改都不会影响其他类。例如,请考虑:

class SelectList
{
    protected $options;
    protected $name;

    public function __construct($name, $options)
    {
         $this->options = $options;
         $this->name = $name;
    }

    public function render()
    {
        $html = "<select name='" . $this->name . "'>\n";
        foreach ($this->options as $option) 
        {
            $html .= $option->render();
        }
        $html .= "</select>\n";
        return $html;
    }
}

class SelectListOption
{
    protected $label;
    protected $value;
    protected $isSelected;

    public function __construct($label, $value, $isSelected = false)
    {
      //Assign the properties
    }

    public function render()
    {
        $html .= '<option value="' . $this->value . '"';
        if ($this->isSelected)
        {
         $html .= ' selected="selected" ';
        }
       $html .= '>' . $this->label . "</option>\n";
    }
}
class选择列表
{
受保护的美元期权;
受保护的$名称;
公共函数构造($name,$options)
{
$this->options=$options;
$this->name=$name;
}
公共职能
{
$html=“\n”;
foreach($this->options as$option)
{
$html.=$option->render();
}
$html.=“\n”;
返回$html;
}
}
类SelectListOption
{
受保护的$标签;
受保护的美元价值;
受保护的选举;
公共函数构造($label,$value,$isSelected=false)
{
//分配属性
}
公共职能
{
$html.=''.$this->label.\n;
}
}
我喜欢以这种方式对事物进行建模的一点是,添加新特性(例如,选定/未选定项的CSS样式,或禁用的属性)非常容易,因为您知道新特性属于哪个对象。此外,拥有这种“小”对象使编写单元测试变得非常容易


HTH

你为什么到处都使用
global
!?这绝对不是OOP。你为什么到处都使用
global
!?这绝对不是OOP。