开发者

Basic class returns object reference instead of Array

I have very basic class:

class Customer {

  protected $id;
  protected $customer;

  public function __construct($customer_id) {
    $this-&g开发者_JAVA技巧t;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);

This is not doing what I want it to but I understand why... $customer returns a reference to the Customer Object...

What I want is the MySQL row array from the mysql_fetch_row() - How do I return the array?

What am I missing?


Of course. If you construct a new object, you'll get back a reference to the new object. How else would you hold the reference to it?

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

The new operator will always return a reference to the newly created object. You can not return anything else from a constructor.


You could leverage ArrayObject (provided you're using PHP 5.3) to get what you want:

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);
    }
}

Then you can use $customer as an array:

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


Sounds like you want a static method instead (judging by the comments) - see the example at the end.

I think you're missing the 'mindset' of the object, however.

class Customer should be an Object - an actual customer. You're halfway there, but you're taking an array and slamming it into a blank object.

Use that set_customer to populate your object - like this (this was typed in-browser btw, just getting the idea across):

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

to fit your code and comments:

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);


Here is what I think is a more Object Oriented Style:

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 );


How about this:

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 );

That said, a few things to mention:

  • The class is missing alot (error/exception handling for one).
  • You may want to look into one of the many ORM's for php.
  • You may want replace the mysql_* use with PDO and use parameterized queries.


Put the result in a public variable within the class and then do a print_r on $customer and don't return in the constructor.

Currently it's a protected variable, so you wont see it with print_r

Does something like this work?

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜