Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-通过父级中的实例化类访问常量_Php_Oop - Fatal编程技术网

php-通过父级中的实例化类访问常量

php-通过父级中的实例化类访问常量,php,oop,Php,Oop,一些代码首先 包含常量的FlashBagUtil类: class FlashBagUtil { const TYPE_NOTICE = 'notice'; const TYPE_WARNING = 'warning'; const TYPE_ALERT = 'alert'; const LANG_EN = 'en'; const LANG_RU = 'ru'; const LANG_IL = 'il'; } 父

一些代码首先

包含常量的FlashBagUtil类:

class FlashBagUtil
{
    const TYPE_NOTICE  = 'notice';
    const TYPE_WARNING = 'warning';
    const TYPE_ALERT   = 'alert';
    const LANG_EN      = 'en';
    const LANG_RU      = 'ru';
    const LANG_IL      = 'il';
}
父类:

class CoreController
{
    public $flashUtil;

    public function __construct()
    {
        $this->flashUtil = new FlashBagUtil;
    }
}
儿童班:

class BatchController extends CoreController
{
    public function indexAction()
    {
        // Method 1 - This works fine
        $flash     = $this->flashUtil;
        $flashType = $flash::TYPE_NOTICE;

        // Method 2 - This, obviously, does not
        $flashType = $this->flashUtil::TYPE_NOTICE;

        // Method 3 - Neither does this as $flashUtil is a non-static instantiated object
        $flashType = self::$flashUtil::TYPE_NOTICE;
    }
}
use TreasureForge\CoreBundle\Util\FlashBagUtil;

class BatchController extends CoreController
{
    public function indexAction()
    {
        $flash = FlashBagUtil::TYPE_NOTICE;
    }
}
PHP文档:声明为静态的属性不能通过实例化的类对象访问(尽管静态方法可以)

但我似乎能用第一种方法做到这一点。我错过了什么

+


方法1是在此上下文中访问静态内容的唯一且最干净的方法吗?

如果要将类用作枚举,请将EnumerationClass抽象为:

abstract FlashBagUtil
{
    const TYPE_NOTICE = 'notice';
    ...
}
并在您的儿童课程中使用:

class Controller
{
    private flashType = FlashBagUtil::TYPE_NOTICE;
}

您正在引用一个类,该类不同于类变量(属性),实例化对象可以访问该类。您正在引用的文档引用了使用
static
关键字(即
private static$flashUtil;
)定义的类变量,如果您习惯于使用其他更严格类型的OOP语言编程,这可能是您困惑的根源。

将其作为建议的抽象类在这里没有多大帮助,我认为,因为我在示例代码中删除了FlashBagUtil类中的更多内容

我的方法1可以工作,但需要复制原始对象,而这违背了公共继承对象的目的。所以

最后,我确定了一种直接访问静态内容的标准方法,方法是将名称空间导入子类,并按照Ralphael的建议使用
$flashType=FlashBagUtil::TYPE_NOTICE
。在一行程序中从对象访问常量会很好,但这样也可以很好地将静态内容分隔开

全日制儿童班:

class BatchController extends CoreController
{
    public function indexAction()
    {
        // Method 1 - This works fine
        $flash     = $this->flashUtil;
        $flashType = $flash::TYPE_NOTICE;

        // Method 2 - This, obviously, does not
        $flashType = $this->flashUtil::TYPE_NOTICE;

        // Method 3 - Neither does this as $flashUtil is a non-static instantiated object
        $flashType = self::$flashUtil::TYPE_NOTICE;
    }
}
use TreasureForge\CoreBundle\Util\FlashBagUtil;

class BatchController extends CoreController
{
    public function indexAction()
    {
        $flash = FlashBagUtil::TYPE_NOTICE;
    }
}

非常感谢您的输入。

您是否尝试过:
FlashBagUtil::在您的子类中键入
注意事项?换句话说,
静态
规则在这里不适用,因为发布的代码中没有定义为
静态
。谢谢,我不确定您的类FlashBagUtil;)