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

Popular posts from this blog

FILE UPLOAD IN PHP BY USEING FORM | PHP FILE UPLOADS

FILE UPLOAD IN PHP  BY USEING FORM | PHP FILE UPLOADS INDEX.PHP //PAGE <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form method="post" action="upload.php" enctype="multipart/form-data"> <input type="file" name="posters" /> <input type="submit" value="upload" name="submit" /> </form> </body> </html> upload.php //PAGE <?php if($_FILES) { $name=$_FILES['posters']['name']; $type=$_FILES['posters']['type']; $extension=array('jpeg','jpg','JPEG','JPG','...