Pass an interface as a function parameter (PHP)?


username

I'm watching one of Jeffs Laracast's tutorials on coding rules.

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

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

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

I don't understand what he is doing here. Did he pass him the interface "Subscription" as a function parameter? In all the previous tutorials about interfaces, I have never seen one.

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

This method requires a parameter called $subscription. This paramater must be a concrete object (or null) that interfaces with the appliance .Subscription

This is done by using so called "type hints" ( http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration ) before arguments .

SubscriptionIt doesn't have to be an interface here - it can also be a class, and the given parameter must be an instance of it Subscriptionor any derived type.

Related


Pass an interface as a function parameter (PHP)?

username I'm watching one of Jeffs Laracast's tutorials on coding rules. function signUp($subscription) { if ($subscription == 'monthly') { $this->createMonthlySubscription(); } elseif ($subscription == 'forever') { $this->

Pass an interface as a function parameter (PHP)?

username I'm watching one of Jeffs Laracast's tutorials on coding rules. function signUp($subscription) { if ($subscription == 'monthly') { $this->createMonthlySubscription(); } elseif ($subscription == 'forever') { $this->

Pass an interface as a function parameter (PHP)?

username I'm watching one of Jeffs Laracast's tutorials on coding rules. function signUp($subscription) { if ($subscription == 'monthly') { $this->createMonthlySubscription(); } elseif ($subscription == 'forever') { $this->

Pass an interface as a function parameter (PHP)?

username I'm watching one of Jeffs Laracast's tutorials on coding rules. function signUp($subscription) { if ($subscription == 'monthly') { $this->createMonthlySubscription(); } elseif ($subscription == 'forever') { $this->

Pass function as parameter in PHP

User 7780894 I've seen someone do this: usort($array, function() { //... }); How can I write a similar implementation with my own functions? E.g: runIt(function() { //... }); and implementation runIt: function runIt() { // do something with pass

Pass function as parameter in PHP

User 7780894 I've seen someone do this: usort($array, function() { //... }); How can I write a similar implementation with my own functions? E.g: runIt(function() { //... }); and implementation runIt: function runIt() { // do something with pass

Pass function as parameter in PHP

User 7780894 I've seen someone do this: usort($array, function() { //... }); How can I write a similar implementation with my own functions? E.g: runIt(function() { //... }); and implementation runIt: function runIt() { // do something with pass

Pass scala function as java function interface parameter

cscan: I have a Java constructor that takes a functional interface as a parameter: public ConsumerService(QueueName queue, Consumer<T> function) { super(queue); this.function = function; } I am trying to use this constructor in Scala, but the compiler

Pass scala function as java function interface parameter

scanning I have a Java constructor that takes a functional interface as a parameter: public ConsumerService(QueueName queue, Consumer<T> function) { super(queue); this.function = function; } I am trying to use this constructor in Scala, but the compil

Pass scala function as java function interface parameter

scanning I have a Java constructor that takes a functional interface as a parameter: public ConsumerService(QueueName queue, Consumer<T> function) { super(queue); this.function = function; } I am trying to use this constructor in Scala, but the compil

Pass scala function as java function interface parameter

cscan: I have a Java constructor that takes a functional interface as a parameter: public ConsumerService(QueueName queue, Consumer<T> function) { super(queue); this.function = function; } I am trying to use this constructor in Scala, but the compiler

PHP interface class in function parameter

sphygmomanometer I want to use a PHP interface like Java interface. How can I make this work in PHP? Android (Java : : btnDownload.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //------------------

PHP interface class in function parameter

sphygmomanometer I want to use a PHP interface like Java interface. How can I make this work in PHP? Android (Java) : btnDownload.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //-------------------

PHP interface class in function parameter

sphygmomanometer I want to use a PHP interface like Java interface. How can I make this work in PHP? Android (Java : : btnDownload.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //------------------

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Pass parameter to function in class in PHP

username I am a beginner trying to learn PHP. I created a class called Image (in a file called image.php) that contains some basic functionality. I'd like to be able to create a "new" image object on the hard drive associated with the jpeg and then pass that f

Pass parameter to function in class in PHP

username I am a beginner trying to learn PHP. I created a class called Image (in a file called image.php) that contains some basic functionality. I'd like to be able to create a "new" image object on the hard drive associated with the jpeg and then pass that f

Pass chain function as parameter as parameter in PHP

Ashok Puddle I have a function. It has method chaining that needs to be executed. public function someFunction() { $query=$this->model; $query->select($columns) ->skip($request->get('start') * $request->get('length')) ->take($reques

Pass chain function as parameter as parameter in PHP

Ashok Puddle I have a function. It has method chaining that needs to be executed. public function someFunction() { $query=$this->model; $query->select($columns) ->skip($request->get('start') * $request->get('length')) ->take($reques

Pass variadic function parameter to another function in PHP

permethrin I want to pass variadic function arguments to one function and foothen foovariadic function arguments to another function bar. At the same time, the initial caller should also be able to call directly barwith variadic function arguments . How can I

Pass function as parameter of another function in PHP

Zby Sheikh I want to pass a function (not the result) as an argument to another function. Suppose I have function double_fn($fn) I want to run a given function ($fn) twice. Is there such a mechanism in PHP? I know you are a kind of variable in a python functi

PHP pass a function as a parameter to another function

Centers for Disease Control and Prevention I have two functions - the first is complex, it takes about 15 seconds to load when it gets data from a 3rd party. The second is normal functionality. To illustrate this is what I have <?php class ComplexFunctions{ p

Pass function as parameter of another function in PHP

Zby Sheikh I want to pass a function (not the result) as an argument to another function. Suppose I have function double_fn($fn) I want to run a given function ($fn) twice. Is there such a mechanism in PHP? I know you are a kind of variable in a python functi

How to pass PHP class as parameter to builder function?

Mostafa: I implemented an object generator that takes a class name and tries to create a new instance object. The builder will try to inject the service if needed. The class name is passed as a string in the current implementation. Here is an example $classNam