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();

松戈将返回错误

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 接口支持常量。它不那么理想,因为您必须记住在每个子类上实现接口,因此它有点部分地破坏了目的。