【问题标题】:calling statically and dynamically same method静态和动态调用相同的方法
【发布时间】:2014-02-07 10:31:53
【问题描述】:

我正在编写一个类似于 orm 的 php 类。 我有一个方法,可以静态调用或实例化,它必须在这两种情况下都有效。 你能看出有什么问题吗。 基本上是一个称为模型的对象。 创建时,它会根据继承的类创建一个表。 例如:

Podcast extends Model ....

有一些像这样的函数需要静态和动态调用。 例如:

$podcastList = Podcast::findAll($db);

我从数据库中获取所有播客对象,而无需实例化播客对象。 但我也可以这样做:

$podcast = new Podcast($db)
$podcastList = $podcast->findAll(); //no db here.... passed before

$db 是我编写的用于对数据库进行操作的类。 IT 简单地处理 OOP,就像 mysql_* 处理函数一样。我没有使用 PDO,我将来可能会使用,但现在我使用 mysql_* :P

那些是有罪的功能

public static function findAll($db=NULL, $self=NULL) {

    if($self == NULL) {

        $self = new static($db);

    } else {

        $self = $this;

    }

    $self->tableName = "";
    $self->db = NULL;

    $is_static = !(isset($this) && get_class($this) == __CLASS__);

    if($is_static) {

        //die(__CLASS__ . "::" . __FUNCTION__ . " CALLED STATICALLY");

        if(!$self->db) {

            die(__CLASS__ . "::" . __FUNCTION__ . " CALLED STATICALLY AND DB IS NULL");
            //It stops here!

        }

        $self->tableName = $self->genTableName();

    } else {

        $self->db = $this->db;
        $self->tableName = $this->tableName;

    }

    $query = "SELECT * FROM {$self->tableName}";

    $r = $self->db->exec($query);

    if(!$r) {

        die(__CLASS__ . ":Error " . __FUNCTION__ . " record: " . $self->db->getError());

    }

    if($self->db->countRows($r) == 0) {

        return NULL;

    }

    $objects = array();

    while($row = $self->db->fetch($r, DBF::FETCH_ASSOC)) {

        $objectClass = __CLASS__;

        $object = new $objectClass($this->db);

        //TODO Do it dinamically indipendently of column name

        $f = get_class_vars($objectClass);

        foreach ($f as $field => $value) {

            $chuncks = explode("_", $field);

            if($chuncks[0] == "f") {

                $object->{$field} = $row[$chuncks[2]];

            }

        }

        $objects[] = $object;

    }

    return $objects;

}

public function __call($name, $arguments) {

    if ($name === 'findAll'){

        return static::findAll($arguments, $this);


    }

}

两者都是类的一部分。

感谢您的帮助!

【问题讨论】:

  • 类中以 f 开头的每个属性都被视为 mysql 表的字段。
  • 例如 $podcast->f_foo 被认为是 podcasts_table.foo 的一个字段

标签: php methods orm static call


【解决方案1】:

这段代码有很多问题。比您的许多逻辑错误(为什么要设置 $self = $this,然后是 $self->db = NULL,然后是 $self->db = $this->db?)更重要的是您误解了能够在 PHP 中动态调用静态函数的含义。对象$this 根本不存在于静态方法中。调用$podcast->findAll() 看起来不是静态的,但它仍然是静态的。

要做你想做的事,这里有一些选项:

  1. 保留函数静态并根据需要调用findAll($this->db, $tablename)
  2. 把函数放到db类中,用参数tablename调用

编辑:

我列表中的第二个是我会怎么做。这是因为您在原始示例中已经有一个 db 对象,并且没有什么特别的东西可以使该函数的目的仅适用于 Podcast 对象,而不适用于表示数据库行的任何其他对象。

//calling examples:
$podcastlist = $db->findAll('Podcast');

$podcast = new Podcast($db);
$podcastlist = $podcast->findAll();


public class db {

    ....

    function findAll($classname, $tablename=NULL) {
        if(!isset($tablename)) {
             //let's pretend you put default table names as class constants
             $tablename = get_constant($classname.'::DEFAULT_TABLE');
        }
        $query = "SELECT * FROM {$tableName}";
        $r = $this->exec($query);
        if(!$r) {
            throw new Exception("Error " . __FUNCTION__ . " record: " . $this->getError());
        }

        if($this->countRows($r) == 0) {
            return NULL;
        }

        $objects = array();
        while($row = $this->fetch($r, DBF::FETCH_ASSOC)) {
            $object = new $classname($this);

            //the following is an easier way to do your original foreach
            foreach($row as $field=>$value) {
                if(property_exists($classname, "f_".$field)) {
                    $object->{'f_'.$field} = $value;
                }
            }
            $objects[] = $object;
        }

        //something you forgot:
        return $objects;
    }
}

public class Podcast extends Model {

    ....

    public function findAll($tablename=NULL) {
        return $this->db->findAll(class_name($this), $tablename);
    }
}

【讨论】:

  • 非常感谢!是的,我对如何管理静态和非静态方法有点困惑。我研究这段代码:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多