K
K
kapai692014-11-25 13:19:23
PHP
kapai69, 2014-11-25 13:19:23

How to pass an interface as a parameter?

There is a class and an interface

<?php
class Test
{
  public function __construct(SomeInterface $someInterface){
      $this->someInterface = $someInterface;	
  }
}

interface SomeInterface 
{
  public function foo($tmp){}
}

How to pass an interface to an instance of the Test class?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2014-11-25
@kapai69

class Test
{
    public function __construct(SomeInterface $someInterface)
    {
        $this->someInterface = $someInterface;
    }
}

interface SomeInterface
{
    public function foo($bar);
}

class SomeClass implements SomeInterface
{
    public function foo($bar)
    {
    }
}

$a = new SomeClass("bar");

$b = new Test($a);

S
Sergey, 2014-11-25
Protko @Fesor

Interface - nothing. An object that implements an interface is easy. Understand the difference and you will be happy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question