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.
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.
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.
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.
An interface can contain methods and constants, but can't contain any variables.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With