puc*_*chu 3 php static class yii
现在我正在使用yii框架,我想写下这样的东西:
protected static $model = "Customer";
...
public function actionIndex() {
$model::model()->find(...
Run Code Online (Sandbox Code Playgroud)
它现在有效:
protected static $model = "Customer";
protected static $model_obj;
...
public function __construct($controller, $id) {
$this->model_obj = new self::$model;
...
public function actionIndex() {
$model_obj::model()->find(...
Run Code Online (Sandbox Code Playgroud)
但是为访问静态成员创建对象是一件坏事.怎么避免呢?
getClass将object作为第一个参数,它不适用于此目的
谷歌说:
$a = constant($myClassName . "::CONSTANT");
$b = call_user_func(array($myClassName, "static_method"));
Run Code Online (Sandbox Code Playgroud)
它看起来像一个可怕的和平的狗屎.使用它可能会带来许多麻烦.另一种方案?
哦!我的问题是另一个:
$controller::$NAME::model() // error
$controller_name = $controller::$NAME
$controller_name::model() // good
Run Code Online (Sandbox Code Playgroud)
谢谢
class foo
{
public static function bar()
{
return 42;
}
}
// class name as string
$class = 'foo';
var_dump($class::bar()); // 42
// method name as string
$method = 'bar';
var_dump(foo::$method()); // 42
// class AND method names as strings
var_dump($class::$method()); // 42
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2868 次 |
| 最近记录: |