繁体   English   中英

PHP 中的抽象常量 - 强制子类定义常量

[英]Abstract constants in PHP - Force a child class to define a constant

我注意到在 PHP 中不能有抽象常量。

有没有办法可以强制子类定义一个常量(我需要在其中一个抽象类内部方法中使用)?

这可能有点“黑客”,但只需很少的努力即可完成这项工作,但如果未在子类中声明常量,则会显示不同的错误消息。

自引用常量声明在语法上是正确的,解析没有问题,只有在运行时实际执行该声明时才会抛出错误,因此抽象类中的自引用声明必须在子类中被覆盖,否则会出现致命错误: Cannot declare self-referencing constant

在这个例子中,抽象的父类Foo强制它的所有子类声明变量NAME 这段代码运行良好,输出Donald 但是,如果子类Fooling声明变量,会触发致命的错误。

<?php

abstract class Foo {

    // Self-referential 'abstract' declaration
    const NAME = self::NAME;

}

class Fooling extends Foo {

    // Overrides definition from parent class
    // Without this declaration, an error will be triggered
    const NAME = 'Donald';

}

$fooling = new Fooling();

echo $fooling::NAME;

constant就是constant 据我所知,PHP 中没有abstractprivate常量,但您可以解决:

示例抽象类

abstract class Hello {
    const CONSTANT_1 = 'abstract'; // Make Abstract
    const CONSTANT_2 = 'abstract'; // Make Abstract
    const CONSTANT_3 = 'Hello World'; // Normal Constant
    function __construct() {
        Enforcer::__add(__CLASS__, get_called_class());
    }
}

这会运行良好

class Foo extends Hello {
    const CONSTANT_1 = 'HELLO_A';
    const CONSTANT_2 = 'HELLO_B';
}
new Foo();

酒吧会返回错误

class Bar extends Hello {
    const CONSTANT_1 = 'BAR_A';
}
new Bar();

Songo会返回错误

class Songo extends Hello {

}
new Songo();

执法者班

class Enforcer {
    public static function __add($class, $c) {
        $reflection = new ReflectionClass($class);
        $constantsForced = $reflection->getConstants();
        foreach ($constantsForced as $constant => $value) {
            if (constant("$c::$constant") == "abstract") {
                throw new Exception("Undefined $constant in " . (string) $c);
            }
        }
    }
}

不幸的是,不是......一个常数正是它在锡上所说的,常数。 一旦定义了就不能重新定义,这样就不可能通过 PHP 的抽象继承或接口来要求它的定义。

但是……您可以检查该常量是否在父类的构造函数中定义。 如果没有,则抛出异常。

abstract class A
{
    public function __construct()
    {
        if (!defined('static::BLAH'))
        {
            throw new Exception('Constant BLAH is not defined on subclass ' . get_class($this));
        }
    }
}

class B extends A
{
    const BLAH = 'here';
}

$b = new B();

这是我从你最初的描述中想到的最好的方法。

不,但您可以尝试其他方法,例如抽象方法:

abstract class Fruit
{
    abstract function getName();
    abstract function getColor();

    public function printInfo()
    {
        echo "The {$this->getName()} is {$this->getColor()}";
    }
}

class Apple extends Fruit
{
    function getName() { return 'apple'; }
    function getColor() { return 'red'; }

    //other apple methods
}

class Banana extends Fruit
{
    function getName() { return 'banana'; }
    function getColor() { return 'yellow'; }

    //other banana methods
}  

或静态成员:

abstract class Fruit
{
    protected static $name;
    protected static $color;

    public function printInfo()
    {
        echo "The {static::$name} is {static::$color}";
    }
}

class Apple extends Fruit
{
    protected static $name = 'apple';
    protected static $color = 'red';

    //other apple methods
}

class Banana extends Fruit
{
    protected static $name = 'banana';
    protected static $color = 'yellow';

    //other banana methods
} 

来源

在 php 7.2 中测试过,但从 5.3 开始,您应该可以利用后期静态绑定来存档此行为。 它会抛出与异常相同的致命错误,因为在大多数情况下,您不想在运行时处理致命错误。 如果您愿意,您可以轻松实现自定义错误处理程序。

所以以下对我有用:

<?php

abstract class Foo {

    public function __construct() {
        echo static::BAR;
    }

}


class Bar extends Foo {
    const BAR = "foo bar";
}

$bar = new Bar();    //foo bar

如果删除const您将得到:

Fatal error: Uncaught Error: Undefined class constant 'BAR' in ...

也许我遗漏了一些东西,但使用后期静态绑定对我有用。 这对你的问题有用吗?

abstract class A{
    const NAME=null;

    static function f(){
        return static::NAME;
    }
}

class B extends A{
    const NAME='B';    
}

B::f();

PHP 接口支持常量。 它并不理想,因为您必须记住在每个子类上实现接口,因此它部分地违背了目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM