简体   繁体   English

PHP OOP问题

[英]PHP OOP Question

Is it possible to require objects in PHP 5.3 to contain certain constants or properties? 是否可以要求PHP 5.3中的对象包含某些常量或属性? I read a bit on PHP's interface model and it seems it only works for abstract public methods so that's right out. 我读了一下PHP的接口模型,看起来它只适用于抽象的公共方法,所以它就是正确的。 So what i'm asking is if there is a way to have two objects Object A and Object B. If Object B wants to extend Object A it must contain a particular constant or variable. 所以我要问的是,是否有一种方法可以有两个对象对象A和对象B.如果对象B想要扩展对象A,它必须包含一个特定的常量或变量。 How would you design this type of architecture? 你会如何设计这种类型的架构? Thanks. 谢谢。

http://php.net/manual/en/language.oop5.interfaces.php#language.oop5.interfaces.constants http://php.net/manual/en/language.oop5.interfaces.php#language.oop5.interfaces.constants

Its possible for interfaces to have constants. 接口有可能有常量。 Interface constants works exactly like class constants except they cannot be overridden by a class/interface that inherits it. 接口常量的工作方式与类常量完全相同,只是它们不能被继承它的类/接口覆盖。

Interface constants cannot be overridden by inheriting classes. 继承类不能覆盖接口常量。 Class constants can be overridden, but are not required to be (even if declared in an abstract class). 类常量可以被重写,但不是必须的 (即使在抽象类中声明)。 Abstract classes are designed to enforce interface , not implementation . 抽象类旨在强制实现 接口 ,而不是实现 Constants fall under implementation, while methods define the interface. 常量属于实现,而方法定义了接口。 Thus, while constants can be declared with a default value in an abstract class, it is up to the child to decide whether to use or redefine them, or not. 因此,虽然常量可以在抽象类中使用默认值声明,但由孩子决定是否使用或重新定义它们。

Your best alternative is to use "getter" methods, eg: 您最好的选择是使用“getter”方法,例如:

abstract class MyAbstract
{
    abstract public function getPropertyA();
    abstract public function getPropertyB();
}

Now, any class using extend MyAbstract will be required to define getPropertyA() and getPropertyB() , which ensures the values will always be accessible. 现在,任何使用extend MyAbstract类都需要定义getPropertyA()getPropertyB() ,以确保始终可以访问值。

Yes and no. 是的,不是。

No, because properties are an implementation detail. 不,因为属性是实现细节。 It is not part of the interface at all (methods define the interface). 它根本不是接口的一部分(方法定义接口)。 You can define properties (in classes) and constants (in classes or interfaces), but you cannot mark them abstract and require them to be "implemented" by derived classes. 您可以定义属性(在类中)和常量(在类或接口中),但是您不能将它们标记为抽象,并要求它们由派生类“实现”。

Yes, because interface allows you to define constants that all derived classes will have as well (be careful with that, see @Gordon's question ). 是的,因为接口允许您定义所有派生类也将具有的常量(请注意, 请参阅@ Gordon的问题 )。 This "enforces" classes to have these constants. 这个“强制”类有这些常量。 You can also "enforce" properties to be passed to derived classes using abstract classes or regular inheritance: 您还可以使用抽象类或常规继承“强制”将属性传递给派生类:

abstract class Foo {
   public $foo = 'bar';
   protected $bar = 'baz';
}

class Bar extends Foo {
   public funciton getBar() {
      return $this->bar;
   }
}

$bar = new Bar;
echo $bar->foo;      // bar
echo $bar->getBar(); // baz

Object A and Object B should be classes, objects are instances ( of classes ) that can't be extended ( i might be mistaken but ... ) . 对象A和对象B应该是类,对象是无法扩展的实例(类)(我可能会弄错但是......)。

With that in mind, you can let's say Class B extends Class A, then if you whant Class B to have a particular constant defined , Class A should implement an interface . 考虑到这一点,你可以说B类扩展了A类,然后如果你让B类定义了一个特定的常量,那么A类应该实现一个接口。 Read more about php interfaces . 阅读更多关于php接口的信息

Hmm, if Object B extends Object A, and Object A contains a constant or variable, then Object B will also contain them if the variable is protected or public . 嗯,如果对象B扩展了对象A,并且对象A包含常量或变量,那么如果变量是protectedpublic ,则对象B也将包含它们。 So if you define the variable in Object A, even if its value is NULL , Object B will by definition also define it. 因此,如果在对象A中定义变量,即使其值为NULL ,对象B也将根据定义定义它。

You cannot force Object B to implement a constant that is not inherited from Object A. Same with an interface -- if B implements interface C, which contains constants, it inherits them. 您不能强制对象B实现不从对象A继承的常量。与接口相同 - 如果B实现包含常量的接口C,则它继承它们。 However, B can define different constant values than its parent class or interface. 但是,B可以定义与其父类或接口不同的常量值。

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

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