简体   繁体   English

如何在子类中创建一个常量,以便在PHP 5.2中的父类中找到的方法中使用?

[英]How can I create a constant in a subclass to be used in a method found in a parent class in PHP 5.2?

Edit: 编辑:
* Note: I'm using PHP 5.2 for the time being, unfortunately. * 注意:遗憾的是,我目前正在使用PHP 5.2。 I can't find a decent cheap host offering 5.3... 我找不到一个体面的廉价主机提供5.3 ...


In PHP, self refers to the class in which the called method is defined. 在PHP中, self指的是定义被调用方法的类。 This means that if you don't override a method in the child class, the keyword self will refer to the parent class, even when called from the child. 这意味着如果您不覆盖子类中的方法,则关键字self将引用父类,即使从子类调用也是如此。

For example, this code: 例如,这段代码:

<?php

class ParentClass {
  const NAME = "ParentClass";
  public function showName() {
    echo self::NAME . "<br />\n";
  }
}

class ChildClass extends ParentClass {
  const NAME = "ChildClass";
  public function __construct() {
    echo self::NAME . "<br />\n";
  }
}

$test = new ChildClass();
$test->showName();

?>

Will create this output: 将创建此输出:

ChildClass
ParentClass

What I want to do is to create a default method (eg showName() in the example above) which exists in a parent class with constants defining default values to use. 我想要做的是创建一个默认方法(例如上面示例中的showName() ),该方法存在于父类中,其中常量定义要使用的默认值。 In the child, I want to be able to override these constants (note the const in the child definition above), and have those values be used when I call the method on an instance of the child. 在子代中,我希望能够覆盖这些常量(注意上面的子定义中的const ),并在我在子实例上调用该方法时使用这些值。

In short, how can I make it so that the output of the above sample would be... 简而言之,我怎样才能使上述样本的输出为......

ChildClass
ChildClass

...without having to duplicate the code of the parent within the child? ...而不必复制孩子中父母的代码?

Try 尝试

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

This uses late static binding : 这使用后期静态绑定

As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance. 从PHP 5.3.0开始,PHP实现了一个称为后期静态绑定的功能,可用于在静态继承的上下文中引用被调用的类。

More precisely, late static bindings work by storing the class named in the last "non-forwarding call". 更确切地说,后期静态绑定通过存储在最后一次“非转发调用”中命名的类来工作。 In case of static method calls, this is the class explicitly named (usually the one on the left of the :: operator); 在静态方法调用的情况下,这是显式命名的类(通常是::运算符左侧的类); in case of non static method calls, it is the class of the object. 在非静态方法调用的情况下,它是对象的类。 A "forwarding call" is a static one that is introduced by self::, parent::, static::, or, if going up in the class hierarchy, forward_static_call(). “转发调用”是由self ::,parent ::,static ::引入的静态调用,或者,如果在类层次结构中上传,则为forward_static_call()。 The function get_called_class() can be used to retrieve a string with the name of the called class and static:: introduces its scope. 函数get_called_class()可用于检索具有被调用类名称的字符串,static ::引入其范围。

EDIT: For 5.2.x 编辑:对于5.2.x

If you don't have 5.3.0 you won't be able to take advantage of this. 如果你没有5.3.0,你将无法利用这一点。 One common hack solution is to create a static cache (eg private static $statics = array() ) referenced by child class name. 一种常见的黑客解决方案是创建由子类名称引用的静态缓存(例如, private static $statics = array() )。 It requires you to track object inheritance to override the value on __construct, and to explicitly define which statics are 'inheritable'. 它要求您跟踪对象继承以覆盖__construct上的值,并明确定义哪些静态是“可继承的”。 For example, SilverStripe uses this technique in the Sapphire ORM to get around PHP static binding limitations. 例如, SilverStripe在Sapphire ORM中使用这种技术来解决PHP静态绑定限制。 They define a base Object class , and various static var management functions. 它们定义了一个基础Object类和各种静态var管理函数。

我相信你的案例的相应语法盐是:

print constant(get_class($this)."::NAME");

使用static::CONSTNAME将返回正确的值。

Maybe it's just because of your short example but the "self" referencing doesn't seem needed. 也许这仅仅是因为你的简短例子,但似乎并不需要“自我”引用。

I would just do this: 我会这样做:

class ParentClass {
  public function showName() {
    echo $this->name() . "<br />\n";
  }

  public static function name() {
    return "ParentClass";
  }
}

class ChildClass extends ParentClass {
  public function __construct() {
    echo $this->name() . "<br />\n";
  }
  public static function name() {
    return "ChildClass";
  }
}

$test = new ChildClass();
$test->showName();

Output: 输出:

ChildClass
ChildClass

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

相关问题 如何在PHP的子类中访问父方法? - How can I access a parent method within a child class in PHP? 如何连接常量和变量并使用PHP将其存储在类常量中? - How can I concatenate a constant and a variable and store it in a class constant with PHP? 在抽象类中,子类可以访问父类的受保护数据/方法吗? - In abstract class subclass can access protected data / method of parent class? 父类的方法如何访问子类的重写变量? - How can a parent class's method access an subclass's overridden variable? 如何仅在 PHP 上的 class inheritance 中运行父方法? - How can I make only the parent method run in a class inheritance on PHP? 如何从已被PHP覆盖的对象的父类中运行方法? - How can I run a method from an object's parent class that has been overridden in PHP? PHP:如何从当前类中使用的特征方法调用父方法? - PHP: How to call parent method from a trait method used in current class? 我可以从父 class 调用子类的构造函数吗? - Can I call a constructor of a subclass from the parent class? 我可以使用常量名称的变量来访问PHP类常量吗? - Can I access a PHP Class Constant using a variable for the constant name? 我可以在Laravel 5.2中创建一个继承自User的新类吗? - Can I create a new class that inherits from User in Laravel 5.2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM