小编saa*_*laa的帖子

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

我正在构建一个重用和简单的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万
查看次数

从常规字符串('')中告诉原始字符串(r'')?

我目前正在构建一个必须将文件名与模式匹配的工具.为方便起见,我打算提供延迟匹配(以类似glob的方式)和regexp匹配.例如,以下两个片段最终会产生相同的效果:

@mylib.rule('static/*.html')
def myfunc():
    pass

@mylib.rule(r'^static/([^/]+)\.html')
def myfunc():
    pass
Run Code Online (Sandbox Code Playgroud)

AFAIK r''仅对Python解析器有用,它实际上str在解析后创建了一个标准实例(唯一的区别在于它保留了\).

有人知道一种方法来告诉彼此吗?

我不想为同一目的提供两个备用装饰器,或者更糟糕的是,手动解析字符串以确定它是否是正则表达式.

python regex string

9
推荐指数
2
解决办法
2160
查看次数

标签 统计

inheritance ×1

php ×1

python ×1

regex ×1

static-methods ×1

string ×1