PHP继承和MySQL [英] PHP Inheritance and MySQL

查看:104
本文介绍了PHP继承和MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用PHP采用好的面向对象编程技术。大部分(全部读完)我的项目涉及MySQL数据库。我的直接问题涉及我需要开发的用户模型。

So I'm trying to adopt good object oriented programming techniques with PHP. Most (read all) of my projects involve a MySQL database. My immediate problem deals with the users model I need to develop.

我当前的项目有代理商和潜在客户。代理商和潜在客户都是拥有大量相同信息的用户。所以,显然,我想要一个类Agents和一个类Leads来扩展一个公共类Users。现在,我的问题如下:

My current project has Agents and Leads. Both Agents and Leads are Users with much of the same information. So, obviously, I want a class Agents and a class Leads to extend a common class Users. Now, my question is as follows:

如何最好地处理SQL以加载这些对象?我实例化代理或潜在客户时,我不想执行多个SQL语句。但是,逻辑告诉我,当触发Users构造函数时,它应该执行一个SQL语句来加载代理和潜在客户之间的公共信息(用户名,密码,电子邮件,联系信息等)。 Logic还告诉我,当激活Agents或Leads构造函数时,我想执行SQL来加载Agents或Leads类的唯一数据....但是,逻辑也告诉我执行2是个坏主意每次我需要代理或潜在客户时的SQL语句(因为每个可能有数千个)。

How should the SQL best be handled for loading these objects? I don't want to execute multiple SQL statements when I instantiate an Agent or a Lead. However, logic tells me that when the Users constructor is fired, it should execute a SQL statement to load the common information between Agents and Leads (username, password, email, contact information, etc). Logic also tells me that when the Agents or Leads constructor is fired, I want to execute SQL to load the data unique to the Agents or Leads class....But, again, logic also tells me that it's a bad idea to execute 2 SQL statements every time I need an Agent or Lead (as there may be thousands of each).

我已经尝试过搜索一般如何处理但没有成功的例子......也许我只是在寻找错误的东西?

I've tried searching for examples of how this is generally handled with no success...Perhaps I'm just searching for the wrong thing?

推荐答案

你基本上有三种解决这个问题的方法(其中一种我会立即消除):

You basically have three approaches to this problem (one of which I'll eliminate immediately):


  1. 每个类一个表(这是我要消除的表);

  2. 带有可选列的记录类型;和

  3. 具有子表的记录类型,具体取决于您加入的类型。

为简单起见,我一般建议(2)。所以,一旦你有了你的表:

For simplicity I generally recommend (2). So once you have your table:

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  type VARCHAR(10),
  name VARCHAR(100)
);

其中type可以是'AGENT'或'LEAD'(例如)。或者,您可以使用一个字符类型代码。然后,您可以开始使用对象模型填充空白:

where type can be 'AGENT' or 'LEAD' (for example). Alternatively you can use one character type codes. You can then start to fill in the blanks with the object model:


  • 您有一个用户父类;

  • 您有两个子课程:领导和代理;

  • 这些孩子有固定类型。

它应该很容易落实到位。

and it should fall into place quite easily.

至于如何加载一个语句,我会使用某种工厂。假设这些准系统课程:

As for how to load in one statement, I would use some kind of factory. Assuming these barebones classes:

class User {
  private $name;
  private $type;
  protected __construct($query) {
    $this->type = $query['type'];
    $this->name = $query['name'];
  }
  ...
}

class Agent {
  private $agency;
  public __construct($query) {
    parent::constructor($query);
    $this->agency = $query['agency'];
  }
  ...
}

class Lead {
  public __consruct($query) {
    parent::constructor($query);
  }
  ...
}

工厂可以看像这样:

public function loadUserById($id) {
  $id = mysql_real_escape_string($id);  // just in case
  $sql = "SELECT * FROM user WHERE id = $id";
  $query = mysql_query($sql);
  if (!query) {
    die("Error executing $sql - " . mysql_error());
  }
  if ($query['type'] == 'AGENT') {
    return new Agent($query);
  } else if ($query['type'] == 'LEAD') {
    return new Lead($query);
  } else {
    die("Unknown user type '$query[type]'");
  }
}

或者,您可以将工厂方法设为静态例如,用户类的方法和/或使用查找表来获取类的类型。

Alternatively, you could have the factory method be a static method on, say, the User class and/or use a lookup table for the types to classes.

也许使用查询结果资源来污染这些类是有问题的在最严格的OO意义上进行设计,但它很简单且有效。

Perhaps polluting the classes with the query result resource like that is a questionable design in the strictest OO sense, but it's simple and it works.

这篇关于PHP继承和MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆