Php interfaces may not include member variables

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


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

public GetField();

asked Feb 12, 2010 at 11:52

Doctor BlueDoctor Blue

3,5876 gold badges38 silver badges60 bronze badges

2

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

answered Feb 12, 2010 at 12:03

Php interfaces may not include member variables

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

answered May 11, 2013 at 3:48

WIMP_noWIMP_no

5105 silver badges7 bronze badges

4

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

answered Apr 18, 2014 at 14:01

1

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.

answered Feb 12, 2010 at 11:54

Noah GoodrichNoah Goodrich

24.4k13 gold badges64 silver badges95 bronze badges

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

answered Feb 12, 2010 at 11:56

Pascal MARTINPascal MARTIN

388k77 gold badges646 silver badges656 bronze badges

Not the answer you're looking for? Browse other questions tagged php interface or ask your own question.

Can interface contain variables PHP?

An interface can contain methods and constants, but can't contain any variables.

Can PHP interface have properties?

PHP - Interfaces vs. Interfaces cannot have properties, while abstract classes can. All interface methods must be public, while abstract class methods is public or protected. All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary.

What are interfaces in PHP?

A PHP interface defines a contract which a class must fulfill. If a PHP class is a blueprint for objects, an interface is a blueprint for classes. Any class implementing a given interface can be expected to have the same behavior in terms of what can be called, how it can be called, and what will be returned.

Why do we use interface in PHP?

Importance of using Interfaces: Interface allows unrelated classes to implement the same set of methods regardless of their positions in the class inheritance hierarchy. Interface enables you to model multiple inheritance.