PHP:我可以在接口(interface)中使用字段吗?

标签 php interface

在 PHP 中,我可以指定一个具有字段的接口(interface),还是 PHP 接口(interface)仅限于函数?

<?php
interface IFoo
{
    public $field;
    public function DoSomething();
    public function DoSomethingElse();
}
?>

如果没有,我意识到我可以在接口(interface)中将 getter 作为函数公开:

public GetField();

最佳答案

您不能指定成员。你必须通过 getter 和 setter 来表明它们的存在,就像你做的那样。但是,您可以指定常量:

interface IFoo
{
    const foo = 'bar';    
    public function DoSomething();
}

http://www.php.net/manual/en/language.oop5.interfaces.php

关于PHP:我可以在接口(interface)中使用字段吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2251541/

相关文章:

linux - 如何将 SocketCAN 配置到特定接口(interface)?

php - 如何在 View Yii 中调用模型函数?

php - Symfony2 - 自定义 DQL 函数已注册但不起作用

PHP、PDO 构建正确的查询以显示最流行的主题

php - 如何在 PHP 中获取 MySQL 表中刚刚插入的行?

java - 具有子类类型的通用接口(interface)实现

delphi - 无法释放接口(interface)实现的类

c++ - 如何将这个工厂逻辑保存在一个地方?

c# - MVC 模式差异

php - 如何使用 FOSRestBundle 以 JSON 格式返回或显示数据