相关疑难解决方法(0)

获取父类中的子类名称(静态上下文)

我正在构建一个重用和简单的ORM库; 一切都很顺利,除了我被一个愚蠢的继承限制所困扰.请考虑以下代码:

class BaseModel {
    /*
     * Return an instance of a Model from the database.
     */
    static public function get (/* varargs */) {
        // 1. Notice we want an instance of User
        $class = get_class(parent); // value: bool(false)
        $class = get_class(self);   // value: bool(false)
        $class = get_class();       // value: string(9) "BaseModel"
        $class =  __CLASS__;        // value: string(9) "BaseModel"

        // 2. Query the database with id
        $row = get_row_from_db_as_array(func_get_args());

        // 3. Return the filled instance
        $obj = new $class(); …
Run Code Online (Sandbox Code Playgroud)

php inheritance static-methods

88
推荐指数
5
解决办法
8万
查看次数

如何在Laravel中返回数据库表名

有没有办法让我可以通过我所在的模型使用当前数据库表?我看到Laravel/Database/Eloquent/model.php中有一个table()函数,但是我调用它从我所在的模型中调用它是不成功的.

laravel eloquent

21
推荐指数
8
解决办法
5万
查看次数

标签 统计

eloquent ×1

inheritance ×1

laravel ×1

php ×1

static-methods ×1