Skip to main content

__call magig method with multiple classes

<?php

 /**
 *
 */
 class address
 {
  public $address=" ramnager";
  function getAddress( )
  {
  return $this->address;
  }
 }
 /**
 *
 */
 class name extends address
 {
  private $firstName="Saddam";
  private $lastName="Hussain";
  public function getfisrtName()
  {
  return $this->firstName;
  }
  public function lastName($value='')
  {
  return $this->lastName;
  }
  public function fullName($value='')
  {
  return " ".$this->firstName." ".$this->lastName;
  }
 }
 /**
 *
 */
 class role
 {
  private $role =" admin";

  public function getRole()
  {
  return $this->role;
  }
 }

 /**
 *
 */
 class user
 {
  protected $newClass;
  protected $newClass1;
  function __construct($newClass,$newClass1)
  {
  $this->newClass =$newClass;
  $this->newClass1 =$newClass1;
  }
  public function __call($method,$parameter)
  {
  if(method_exists($this->newClass, $method)){
  return call_user_func_array(array($this->newClass,$method),$parameter);
  }elseif(method_exists($this->newClass1, $method)){
  return call_user_func_array(array($this->newClass1,$method),$parameter);
  }else {
  echo "this method dose not exists.";
  }
  }
 }

 $newUser= new user(new name,new role);
 echo $newUser->fullName();
 echo $newUser->getRole();
 echo $newUser->getAddress();

Comments