【问题标题】:how do I make this function a class?如何使这个函数成为一个类?
【发布时间】:2010-11-26 17:01:38
【问题描述】:

我已经创建函数太久了,但没有将我的代码放到“类”中。

我通过例子学得很好,我只是想把这个简单的函数转换成一个类,这样我就可以比较我知道的东西和我不知道的东西......

取如下函数:

function affiliateName($affiliateID) {

$sql = 'SELECT * FROM affiliates WHERE uID="' . $affiliateID . '" ';

$res = mysql_query($sql);

$row = mysql_fetch_array($res);

$affiliateName = $row['firstName'] . ' ' . $row['lastName'];

return $affiliateName;

}

那我怎么把它变成一门课呢?

【问题讨论】:

  • 你想创建一个类定义吗?不是吗?

标签: php function class


【解决方案1】:
<?php
class AffiliateModel
{
    public function first($id)
    {
        $sql = 'SELECT *, CONCAT(firstName, ' ', lastName) AS qualifiedName FROM affiliates WHERE uID="' . $id . '" LIMIT 1';
        $res = mysql_query($sql);

        return mysql_fetch_object($res);
    }
}

$model = new AffiliateModel();
$a = $model->first($id);
echo $a->qualifiedName;
?>

【讨论】:

    【解决方案2】:

    希望对你有帮助

    <?php
    class affiliate{
    
      // fields or properties
      public $name = '';
      public $id = 0;
    
      // constructor
      public function affiliate($id = 0){
         $this->set_ID($id);
      }
    
      // methods
      public function set_ID($id){
        return $this->id = $id;
      }
    
      public function get_Name(){
    
        if($this->name != ""){
          $sql = 'SELECT * FROM affiliates WHERE uID="' . $this->id . '" ';
          $res = mysql_query($sql);
          $row = mysql_fetch_array($res);
          return $this->name = $row['firstName'] . ' ' . $row['lastName'];
        }else{
          return $this->name;
        }
      }
    }
    
    // Example:
    
      $currentAffiliate = new affiliate(153);
      echo $currentAffiliate->name;
    ?>
    

    【讨论】:

      【解决方案3】:

      我更喜欢以下设计,因为它使用起来最简单:

      class affiliates {
          static function load($id) {
              return new self(intval($id));
          }
          private function __construct($id) {
              $query = "SELECT * FROM affiliates WHERE id = " . intval($id);
              $result = mysql_query($query);
              // TODO: make sure query worked
              foreach(mysql_fetch_assoc($result) as $field => $value)
                  $this->$field = $value;
          }
          // composite fields - made by combining and/or re-formatting other fields
          function qualifiedName() {
              return $this->firstName . ' ' . $this->lastName;
          }
          function properName() {
              return $this->lastName . ', ' . $this->firstName;
          }
      }
      
      $a = affiliates::load(22);
      // all db fields are available as properties:
      echo $a->id; // 22
      echo $a->firstName; // Fred
      echo $a->lastName; // Smith
      // composite fields are methods
      echo $a->qualifiedName(); // Fred Smith
      echo $a->properName(); // Smith, Fred
      // to get a single field from a particular person
      echo affiliates::load(72)->qualifiedName();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多