Basic类返回对象引用而不是Array

时间:2010-05-27 01:43:19

标签: php class arrays object

我有非常基本的课程:

class Customer {

  protected $id;
  protected $customer;

  public function __construct($customer_id) {
    $this->id = $customer_id;
    return $this->set_customer();
  }

  protected function set_customer() {
    $query = mysql_query("SELECT * FROM customer WHERE id = '$this->id'");
    $this->customer = mysql_fetch_row($query);
    return $this->customer;
  }
}

$customer = new Customer($order->customer->id);
print_r($customer);

这不是我想要的,但我理解为什么...... $ customer返回对客户对象的引用......

我想要的是来自mysql_fetch_row()的MySQL行数组 - 如何返回数组?

我错过了什么?

6 个答案:

答案 0 :(得分:3)

当然。如果构造一个新对象,则会返回对新对象的引用。你怎么能保留它的参考?

$customer = new Customer();
// if $customer was a data array, where did the object reference go?

new运算符将始终返回对新创建的对象的引用。你不能从构造函数中返回任何其他内容。

答案 1 :(得分:2)

您可以利用ArrayObject(假设您使用的是PHP 5.3)来获得您想要的内容:

class Customer extends ArrayObject {

    protected $id;
    protected $customer;

    public function __construct($customer_id) {
        $this->id = $customer_id;
        $this->set_customer();
        parent::__construct($this->customer);
    }

    protected function set_customer() {
       $query = mysql_query("SELECT * FROM customer WHERE id = '$this->id'");
       $this->customer = mysql_fetch_row($query);
    }
}

然后您可以将$customer用作数组:

$customer = new Customer($customer_id);
echo $customer['name'];

答案 2 :(得分:2)

听起来你想要一个静态方法(根据评论判断) - 最后看一下例子。

但是,我认为你错过了对象的'心态'。

类客户应该是一个对象 - 一个真正的客户。你已经到了一半,但是你正在拿一个阵列把它撞成一个空白物体。

使用set_customer来填充你的对象 - 就像这样(这是在浏览器中键入的顺便说一下,只是想到了这个想法):

class Customer {

  public $id;
  public $name;
  public $address;

  public function __construct($customer_id) {
    $this->customer = $this->set_customer($id);
  }

  private function set_customer($id ) {
    $query = mysql_query("SELECT * FROM customer WHERE id = '$id'");
    $customer = mysql_fetch_assoc($query);
    foreach($customer as $field => $value) {
       $this->$field = $value;
    }
  }
}

$customer = new Customer();
print_r($customer); // now has $customer->id, $customer->name, $customer->address

以适合您的代码和评论:

class Customer {

  protected $id;
  public $customer;

  public function __construct($customer_id) {
    $this->id = $customer_id;
    $this->customer = self::get_customer($id);
  }

  public static function get_customer($id ) {
    $query = mysql_query("SELECT * FROM customer WHERE id = '$id'");
    return mysql_fetch_row($query);
  }
}

$customer = Customer::get_customer($id);

答案 3 :(得分:1)

这个怎么样:

class Customer {

  protected $id;

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

  public function getAsArray() {
    $query = mysql_query("SELECT * FROM customer WHERE id = '$this->id'");
    return mysql_fetch_assoc($query);
  }
}

$customer = new Customer( $id );
$customer_array = $customer->getAsArray();
print_r( $customer_array );

那就说了几件事:

  • 该类缺少很多(错误/异常处理)。
  • 你可能想要查看php的许多ORM's中的一个。
  • 您可能希望将mysql_ *替换为PDO并使用参数化查询。

答案 4 :(得分:1)

以下是我认为更面向对象的风格:

class CustomerFind {

    public static function byIdNumber($customer_id) {
      $query = mysql_query("SELECT * FROM customer WHERE id = '$customer_id'");
      return mysql_fetch_assoc($query);
    }

}

$customer = CustomerFind::byIdNumber( 1 );
print_r( $customer );

答案 5 :(得分:0)

将结果放在类中的公共变量中,然后在$ customer上执行print_r,不要在构造函数中返回。

目前它是受保护的变量,所以你不会用print_r

看到它

这样的事情有用吗?

class Customer {

  protected $id;
  public $customer;

  public function __construct($customer_id) {
    $this->id = $customer_id;
    $this->set_customer();
  }

  protected function set_customer() {
    $query = mysql_query("SELECT * FROM customer WHERE id = '$this->id'");
    $this->customer = mysql_fetch_row($query);
  }
}

$customer = new Customer($order->customer->id);
print_r($customer);
相关问题