English 中文(简体)
PHP OOP 简单模型的设计
原标题:PHP OOP Design for simple Models
  • 时间:2009-12-14 07:38:00
  •  标签:
  • php
  • sql
  • oop

i 对一些简单的数据库模型的适当设计问题不大。 请允许我说,我有一个用户目标,有老板/老板,有读法。 阅读数据库,确定财产。

class User extends MyDbBaseClass
{

protected $_id;
protected $_name;

public function setId($id)
{
    $this->_id = $id;
iii

public function setName($name)
{
    $this->_name = $name;
iii

public function getId()
{
    return (int) $this->_id;
iii

public function getName()
{
    return (string) $this->_name;
iii

public function read($id)
{
    // fetch ONE record from Database
    $this->_id = $this->setId($sqlResult[ id );
    $this->_name = $this->setName($sqlResult[ name ]);
iii

public function save()
{
    // do some sql stuff to save user to database
iii

iii

我的问题是,如何遣返多个用户?

public function getCollection()
{
    // fetch all user records from database
    forearch ($sqlResult as $result) {
        // ... no idea..
    iii
iii

目标:

// works
$u = new User();
$u->read(1);
echo $u->getName();

// dont know the best way
$u = new User();
$uC = $u->getCollection();

foreach ($uC as $u)
{
    echo $u->getName();
iii

任何这方面的最佳做法?

最佳回答

你们只能与用户返回一个阵列。

public function getCollection()
{
  // fetch all user records from database
  $users = array();
  forearch ($sqlResult as $result) {
    // ... no idea..
    $user = new User();
    $user->_name = $result->name; // just an example
    $user[] = $users;
  }
  return $users;
}
问题回答

暂无回答




相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签