PHP: Can I Use Fields In Interfaces?

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();
}
?>

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

public GetField();

Is there a good use case for fields in interfaces today?

Java allows fields in interfaces. This had some use before java 5. Do they have any good use case today? Can Someone give me some good use cases where one would use fields in interfaces as opposed to

When to use Interfaces in PHP

I have always had a hard time understanding the real value of Interfaces when coding with Objects in PHP (could be other languages I imagine) From what I understand you use an Interface to enforce or

Who/What implements Interfaces that I can use directly?

I came across several interfaces while learning JDBC – Connection, Statement, ResultSet etc… Does this imply that some classes somewhere, hidden from me, are implementing these interfaces, and prov

Can I use Spec to build user interfaces in Seaside?

I have noted in the examples package ‘Spec-Examples-PolyWidgets’ that is possible to build user interfaces using Polymorph. Could do the same for the Seaside web framework?

Can I use more generic interfaces to simplify my classes to use a command pattern?

I’m trying to make an app I’m designing more generic and implement the command pattern into it to use manager classes to invoke methods exposed by interfaces. I have several classes with the GetItem()

Why PHP Trait can’t implement interfaces?

I’m wondering why PHP Trait (PHP 5.4) cannot implement interfaces. I understand that it could be obvious, because people could think that if a Class A is using a Trait T which is implementing an inter

Why should I create Interfaces in PHP? [duplicate]

Possible Duplicate: What is the point of interfaces in PHP? Why should I create Interfaces in PHP? As I understands, interfaces are there to describe classes that implement them. The classes have to

How can I meaningfully use precondition contracts in D interfaces?

When I override functions in D with in contracts, then the inherited in contracts are checked. If they fail, then the overridden in contracts are checked. If I don’t specify any in contract, the

How can I create implementations of interfaces, that are use specific implementations of other interfaces?

I definitely could not explain what I mean in the title. The issue is either too stupid or too complicated, but the bottom end is that I could not find an appropriate design pattern for it. So to the

PHP Namespaces and interfaces

I am trying to use namespaces in php with some classes and interfaces. It appears that I must put a use statement for both the interface and the concrete type being used. This is kind of defeating the

Answers

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.

Publicly accessible properties would actually violate encapsulation of data within the class that implements the interface.

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)

In PHP, trying to define properties in an interface should raise a Fatal Error : this portion of code :

interface A {
  public $test;
}

Will give you :

Fatal error: Interfaces may not include member variables in...

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();
}

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

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. 🙂

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!

As of PHP 5.4 you can use traits to provide fields and methods to classes, ie:

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');
    }
}