Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing interface as function parameter (PHP)?

I am watching one of Jeffs Laracast Tutorials about coding rules.

function signUp($subscription) 
{
    if ($subscription == 'monthly')
    {
        $this->createMonthlySubscription();
    }
    elseif ($subscription == 'forever')
    {
        $this->createForeverSubscription();
    }
 }

He wants to use polymorphism and interfaces here. He changes the above code to:

function signUp(Subscription $subscription)
{
    $subscription->create();
}

I don't understand what he is doing here. Is he passing the interface "Subscription" as a function parameter..? I never saw this in all previous tutorials about interfaces.

like image 391
user838531 Avatar asked Apr 28 '16 11:04

user838531


People also ask

What is parameter passing in PHP?

Introduction. In PHP, arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.

What are function parameters in PHP?

PHP Parameterized functions are the functions with parameters. You can pass any number of parameters inside a function. These passed parameters act as variables inside your function. They are specified inside the parentheses, after the function name.

Can we define method in interface in PHP?

An Interface enables us to make programs, indicating the public methods that a class must execute, without including the complexities and procedure of how the specific methods are implemented. This implies that an interface can define method names and arguments, but not the contents of the methods.

CAN interface have variables in PHP?

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


2 Answers

function signUp(Subscription $subscription)
{
    $subscription->create();
}

This methods expects a single paramater called $subscription. This paramater has to be a concrete object (or null) that implements the Subscription interface.

This is done via a so called "type hint" (http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration) before the parameter.

Subscription does not need to be an interface here - it could also be a class, and the given parameter must either be an instance of Subscription or any derived type.

like image 195
StandByUkraine Avatar answered Sep 27 '22 21:09

StandByUkraine


here is the detail explanation according to your given case I hope so after that you will understand this concept properly


Interface Subscription{
    public function create();
}

class MonthlySubscription implements Subscription{

    public function create(){
        print_r("this is monthly subscription create method");
    }

}

class ForeverSubscription implements Subscription{

public function create(){
        print_r("this is yearly subscription create method");
    }   

}


class user {

public function signUp(Subscription $subscription){

    $subscription->create();

}

public function getSubcriptionType($type){

if($type=='forever'){

    return new ForeverSubscription;

}

    return new MonthlySubscription;

}


}

$user=new User();

$subscription=$user->getSubcriptionType('forever');

$user->signUP($subscription);


public function signUp(Subscription $subscription){
    $subscription->create();
}

In this method you are trying to do method injection
method injection means passing a dependency(instance/refference/object etc) into a method

in Singup(Subscription $subscription) mentod


Subscription is a 'type hint'
which ensure that object that will be pass to signUp() function must be an instance of that class who implemented 'Subscription ' interface

like image 26
shafeeque ahmad Avatar answered Sep 27 '22 20:09

shafeeque ahmad