PHP:我可以在接口中使用字段吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2251541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Can I Use Fields In Interfaces?
提问by Doctor Blue
In PHP, can I specify an interface to have fields, or are PHP interfaces limited to functions?
在 PHP 中,我可以指定一个接口来拥有字段,还是 PHP 接口仅限于函数?
<?php
interface IFoo
{
public $field;
public function DoSomething();
public function DoSomethingElse();
}
?>
If not, I realize I can expose a getter as a function in the interface:
如果没有,我意识到我可以在接口中将 getter 作为函数公开:
public GetField();
回答by Gordon
You cannot specify members. You have to indicate their presence through getters and setters, just like you did. However, you can specify constants:
您不能指定成员。你必须像你一样通过 getter 和 setter 来表明它们的存在。但是,您可以指定常量:
interface IFoo
{
const foo = 'bar';
public function DoSomething();
}
See http://www.php.net/manual/en/language.oop5.interfaces.php
回答by WIMP_no
Late answer, but to get the functionality wanted here, you might want to consider an abstract class containing your fields. The abstract class would look like this:
迟到的答案,但要获得此处所需的功能,您可能需要考虑包含您的字段的抽象类。抽象类看起来像这样:
abstract class Foo
{
public $member;
}
While you could still have the interface:
虽然您仍然可以拥有界面:
interface IFoo
{
public function someFunction();
}
Then you have your child class like this:
然后你的孩子类是这样的:
class bar extends Foo implements IFoo
{
public function __construct($memberValue = "")
{
// Set the value of the member from the abstract class
$this->member = $memberValue;
}
public function someFunction()
{
// Echo the member from the abstract class
echo $this->member;
}
}
There's an alternative solution for those still curious and interested. :)
对于那些仍然好奇和感兴趣的人,还有另一种解决方案。:)
回答by Noah Goodrich
Interfaces are only designed to support methods.
接口仅设计用于支持方法。
This is because interfaces exist to provide a public API that can then be accessed by other objects.
这是因为接口的存在是为了提供一个公共 API,然后其他对象可以访问该 API。
Publicly accessible properties would actually violate encapsulation of data within the class that implements the interface.
可公开访问的属性实际上会违反实现接口的类中的数据封装。
回答by Noah Goodrich
Use getter setter. But this might be tedious to implement many getters and setters in many classes, and it clutter class code. And you repeat yourself!
使用 getter 设置器。但是,在许多类中实现许多 getter 和 setter 可能会很乏味,并且会使类代码变得混乱。而你又重复了一遍!
As of PHP 5.4 you can use traitsto provide fields and methods to classes, ie:
从 PHP 5.4 开始,您可以使用特征为类提供字段和方法,即:
interface IFoo
{
public function DoSomething();
public function DoSomethingElse();
public function setField($value);
public function getField();
}
trait WithField
{
private $_field;
public function setField($value)
{
$this->_field = $value;
}
public function getField()
{
return $this->field;
}
}
class Bar implements IFoo
{
use WithField;
public function DoSomething()
{
echo $this->getField();
}
public function DoSomethingElse()
{
echo $this->setField('blah');
}
}
This is specially usefull if you have to inherit from some base class and need to implement some interface.
如果您必须从某个基类继承并需要实现某个接口,这将特别有用。
class CooCoo extends Bird implements IFoo
{
use WithField;
public function DoSomething()
{
echo $this->getField();
}
public function DoSomethingElse()
{
echo $this->setField('blah');
}
}
回答by Pascal MARTIN
You cannot specify properties in an interface: only methods are allowed (and make sense, as the goal of an interface is to specify an API)
您不能在interface: 中指定属性,只允许方法(并且有意义,因为接口的目标是指定 API)
In PHP, trying to define properties in an interface should raise a Fatal Error : this portion of code :
在 PHP 中,尝试在接口中定义属性应该会引发致命错误:这部分代码:
interface A {
public $test;
}
Will give you :
会给你 :
Fatal error: Interfaces may not include member variables in...

