【问题标题】:Do child classes inherit parent constants and if so how do I access them?子类是否继承父常量,如果是,我如何访问它们?
【发布时间】:2014-01-29 11:37:38
【问题描述】:

问题说明了一切。

我在父类中定义了常量。我试过$this->CONSTANT_1,但它不起作用。

class MyParentClass{

    const CONSTANT_1=1
}

class MyChildClass extends MyParentClass{

 //want to access CONSTANT_1  

}

【问题讨论】:

  • 试试 $my_child_class_instance::CONSTANT_1
  • 你用const定义了一个类常量,而不是一个使用define关键字的常量。因此,您的 类常量 属于该类,而不是该类的实例。从逻辑上讲,这就是$this->CONSTANT_1 失败的原因。 $this 只能访问 instance 成员,并且您的类常量不属于该系列。您要使用的是self:: 而不是$this
  • 注意:如果您使用Interface 来集中定义类常量,则不能随后在子类中覆盖它们。 php.net/manual/en/language.oop5.interfaces.php
  • @LauriElias 您的建议将不起作用,因为使用范围解析运算符 (::) 对类级别成员的基本引用不得以美元符号进行。 MyParentClass::,而不是 $MyParentClass:: ;-) 在子类中使用 self 关键字是可行的方法,尤其是在 PHP 7.1+ 中,类常量可以具有可见性修饰符。
  • @AnthonyRutledge 除了你的死灵倾向之外,它确实有效:tehplayground.com/xEnBzy6fxprF4THw

标签: php class oop


【解决方案1】:

我认为您需要像这样访问它:

self::CONSTANT_1;

或者“parent”,它总是在父类中建立的值(即,保持常量的不变性):

parent::CONSTANT_1;

有趣

值得注意的一点是,您实际上可以覆盖子类中的 const 值。

class MyParentClass{

    const CONSTANT_1=1;
}

class MyChildClass extends MyParentClass{

    const CONSTANT_1=2;
}

echo MyParentClass::CONSTANT_1; // outputs 1
echo MyChildClass::CONSTANT_1; // outputs 2

【讨论】:

  • OP 没有询问客户端代码如何访问类常量。这是子类中的可见性问题。 self 是要走的路。
  • 对于通过 google 来到这里的任何人,请查看后期静态绑定的概念。 self::CONSTANT 和 static::CONSTANT 产生不同的结果。
【解决方案2】:

您想使用self 关键字。

class Whale
{
    const BLOWHOLES = 1;
}

class BlueWhale extends Whale
{ 
    /**
     * A method that does absolutely nothing useful.
     */
    public function funnyCalculation()
    {
        return self::BLOWHOLES + 2;  // This is the access you are looking for.
    }
}

查看ways to access class constants inside and outside of the class definition 的 PHP 手册。

【讨论】:

    【解决方案3】:

    您不必使用parent。您可以使用self,它会首先检查class 本身中是否存在同名的constant,然后它会尝试访问parents constant

    所以self 更加通用,并提供了“覆盖”parentsconstant 的可能性,而无需实际覆盖它,因为您仍然可以通过parent:: 显式访问它。

    以下结构:

    <?php
    
    class parentClass {
    
        const MY_CONST = 12;
    
    }
    
    class childClass extends parentClass {
    
        public function getConst() {
            return self::MY_CONST;
        }
    
        public function getParentConst() {
            return parent::MY_CONST;
        }
    
    }
    
    class otherChild extends parentClass {
    
        const MY_CONST = 200;
    
        public function getConst() {
            return self::MY_CONST;
        }
    
        public function getParentConst() {
            return parent::MY_CONST;
        }
    }
    

    导致以下结果:

    $childClass = new childClass();
    $otherChild = new otherChild();
    
    
    echo childClass::MY_CONST; // 12
    echo otherChild::MY_CONST; // 200
    
    echo $childClass->getConst(); // 12
    echo $otherChild->getConst(); // 200
    
    echo $childClass->getParentConst(); // 12
    echo $otherChild->getParentConst(); // 12
    

    【讨论】:

    • 在给出的答案中,在子类中使用self 是最好的答案。请注意,在 PHP 7.1+ 中,您可以将可见性修饰符添加到常量(public、protected、private)。因此,self(对类的引用,自身)比以往任何时候都更重要。但请注意,OP 问题与访问客户端代码中的常量无关。
    【解决方案4】:

    您还可以使用 static 键从父方法访问子方法中的常量定义。

    <?php
    
    class Foo {
    
        public function bar() {
    
            var_dump(static::A);
        }
    }
    
    class Baz extends Foo {
    
        const A = 'FooBarBaz';
    
        public function __construct() {
    
            $this->bar();
        }
    }
    
    new Baz;
    

    【讨论】:

    • 为什么是“要走的路”?你可以解释吗 ?我看到它完全相反,例如,如果您的抽象类有一个方法并且调用 self 将引用来自父级的常量值,而 static 非常了解继承并将输出子类值..
    【解决方案5】:
    <?php
    class MyParentClass{
        const CONSTANT_1=123;
    }
    
    class MyChildClass extends MyParentClass{
    
        public static function x() {
            echo parent::CONSTANT_1;
        }
    
    }
    
    MyChildClass::x();
    

    现场示例:http://codepad.org/Yqgyc6MH

    【讨论】:

      【解决方案6】:

      使用parent,例如:

      class MyParentClass{
      
          const CONSTANT_1=1;
      }
      
      class MyChildClass extends MyParentClass{
      
         function __construct(){
            echo parent::CONSTANT_1;  //here you get access to CONSTANT_1 
         }
      
      }
      
      new MyChildClass();
      

      或者:

       class MyParentClass{
      
              const CONSTANT_1=1;
          }
      
          class MyChildClass extends MyParentClass{
      
            MyParentClass::CONSTANT_1; // here you you get access to CONSTANT_1 too
      
          }
      

      【讨论】:

      • 好的,所以你不能像访问父变量一样访问它们,例如$this->CONSTANT_1?
      • 当然是什么意思?如果它像“当然”一样简单,我就不会问了。您a=可以通过这种方式访问​​父变量。
      • 变量,但不是常量和静态方法/属性
      • 这正是我不知道的。我知道静态方法和属性,但不知道常量。
      • Nicola,$this 在对象内部使用 - 即类的实例 - 并引用对象本身。
      猜你喜欢
      • 2020-02-18
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 2021-10-22
      • 1970-01-01
      • 2012-05-21
      相关资源
      最近更新 更多