PHP: Can I Use Fields In Interfaces?

PhpInterface

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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

Solution 2 - 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. :)

Solution 3 - Php

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

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

Solution 4 - Php

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.

Solution 5 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDoctor BlueView Question on Stackoverflow
Solution 1 - PhpGordonView Answer on Stackoverflow
Solution 2 - PhpWIMP_noView Answer on Stackoverflow
Solution 3 - Phpuser133408View Answer on Stackoverflow
Solution 4 - PhpNoah GoodrichView Answer on Stackoverflow
Solution 5 - PhpPascal MARTINView Answer on Stackoverflow