PHP:我可以在接口中使用字段吗? [英] PHP: Can I Use Fields In Interfaces?

查看:77
本文介绍了PHP:我可以在接口中使用字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,我可以指定一个接口以包含字段,还是PHP接口仅限于功能?

In PHP, can I specify an interface to have fields, or are PHP interfaces limited to functions?

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

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

If not, I realize I can expose a getter as a function in the interface:

public GetField();

推荐答案

您无法指定成员.就像您一样,您必须通过getter和setter指示它们的存在.但是,您可以指定常量:

You cannot specify members. You have to indicate their presence through getters and setters, just like you did. However, you can specify constants:

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

请参见 http://www.php.net/manual/zh-CN /language.oop5.interfaces.php

这篇关于PHP:我可以在接口中使用字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆