从继承的获取静态函数
get static function from inherited
当我使用
DemoStyle::order(); // returns {{%demo_style}} but not 'site.site_demo_style'
DemoLayout::order(); // returns {{%demo_layout}} but not 'site.site_demo_layout'
我弄错了 table 名字。
我相信这是 ActiveRecord::tableName() returns 错误的名称。
如何从 DemoStyle 和 DemoLayout 获取 tableName。
class Sortable extends ActiveRecord
{
public static function order()
{
return self::tableName();
}
}
class DemoStyle extends Sortable
{
public static function tableName()
{
return 'site.site_demo_style';
}
}
class DemoLayout extends Sortable
{
public static function tableName()
{
return 'site.site_demo_layout';
}
}
请不要投反对票。
更新
为什么要使用静态方法?当你定义一个抽象方法并且不使用静态上下文时,这对我有用:
<?php
abstract class Sortable
{
public abstract function tableName();
public function order()
{
return $this->tableName();
}
}
class DemoStyle extends Sortable
{
public function tableName()
{
return 'site.site_demo_style';
}
}
class DemoLayout extends Sortable
{
public function tableName()
{
return 'site.site_demo_layout';
}
}
echo (new DemoStyle)->order();
更新 2
php 静态上下文文档中的示例:Late Static Bindings
在这种情况下有效
static::tableName()
当我使用
DemoStyle::order(); // returns {{%demo_style}} but not 'site.site_demo_style'
DemoLayout::order(); // returns {{%demo_layout}} but not 'site.site_demo_layout'
我弄错了 table 名字。 我相信这是 ActiveRecord::tableName() returns 错误的名称。 如何从 DemoStyle 和 DemoLayout 获取 tableName。
class Sortable extends ActiveRecord
{
public static function order()
{
return self::tableName();
}
}
class DemoStyle extends Sortable
{
public static function tableName()
{
return 'site.site_demo_style';
}
}
class DemoLayout extends Sortable
{
public static function tableName()
{
return 'site.site_demo_layout';
}
}
请不要投反对票。
更新
为什么要使用静态方法?当你定义一个抽象方法并且不使用静态上下文时,这对我有用:
<?php
abstract class Sortable
{
public abstract function tableName();
public function order()
{
return $this->tableName();
}
}
class DemoStyle extends Sortable
{
public function tableName()
{
return 'site.site_demo_style';
}
}
class DemoLayout extends Sortable
{
public function tableName()
{
return 'site.site_demo_layout';
}
}
echo (new DemoStyle)->order();
更新 2
php 静态上下文文档中的示例:Late Static Bindings
在这种情况下有效
static::tableName()