Question: Why should we use class name in another class constructor function in php7?

I was working around oops in php 7 ,in which I pass class name and variable to another class constructor function to access the function of it Like inheritance concept.Let me brief with example

Example for passing class name and variable in constructor  :

class A{
   public function execute($user){
   echo $user;
  }
}
class B {
  protected $a;
//where we pass Class A  inside constructor
  public function __construct(A $a){
     $this->a = $a;
  }
 public function show(){
    $user = 'pramodh';
    $this->a->execute($user);
   }
}
$b = new B(new A);
$b->show();


Out Put :
Pramodh


Example for passing just variable  in constructor  :

class A{
   public function execute($user){
   echo $user;
  }
}
class B {
  protected $a;
//where we pass Class A  inside constructor
  public function __construct($a){
     $this->a = $a;
  }
 public function show(){
    $user = 'pramodh';
    $this->a->execute($user);
   }
}
$b = new B(new A);
$b->show();

Out Put :
Pramodh

Because I read some blog mentioning that we can't  use it ,while working on multiple class.Because we have to change the class name in construct.If we don't need to mention class name in construct whats the problem in it.Please let me know if I am wrong.

Comments

Popular posts from this blog

create pdf by using javascript

yii framework simple shopping cart tutorial for beginners

yii2 arrayhelper::map vs array_map