-1

I'm trying to get the method using call_user_func in PHP but the fact is I can't get it in proper way. My data is stored into another class method and I would like to fetch that data into another class method. First of all I've create an object with some param which are given in other class. Based on the param the class starts it's work than store the data into the method. My problem is I've already called an object after class now how can I call that object again into another class. Maybe you can understand it by the sample code:

class A{
    public $a;
    public $b;
    public function __construct(){

    }
    public function x(){
        $this->a = "something";
        $this->b = "something1";
    }
    public function abc(){
        $printthis = call_user_func(array($obj2,$method_name);
        echo $printthis;
    }
}
$obj1 = new A;
$obj1->x;

class B{
    public $a;
    public $b;
    public function __construct($c,$d){
        $this->a = $c;
        $this->b = $d;
    }
    public function abc(){
        //some code here that returns something
    }
    public function abd(){
        //some code here that returns something
    }
}
$obj2 = new B($obj1->a, $obj1->b);

In the above code section I don't know how to get that data from B class into A class. Can anyone help me in this topic? I'm getting nothing from here. A code example can explain everything.

mkj
  • 2,761
  • 5
  • 24
  • 28
Adnan Shawkat
  • 188
  • 1
  • 12

3 Answers3

1

Basic inheritance will resolve all your issues.

class A{
    public $a;
    public $b;
    public function __construct(){

    }
    public function x(){
      $this->a = "something";
      $this->b = "something1";
   }
   public function abc(){
    $printthis = call_user_func(array($obj2,$method_name);
    echo $printthis;
   }

   // to call abd() method from class B inside this class
   function call_child_method(){
    if(method_exists($this, 'abd')){
        $this->abd();
    }
}
}
$obj1 = new A;
$obj1->x();

class B extends A{  // object will can call both methods of class A and B
   public $a;
   public $b;
   public function __construct($c,$d){
      $this->a = $c;
      $this->b = $d;
   }
   public function abc(){
      //some code here that returns something
   }
   public function abd(){
     //some code here that returns something
   }

   // to call X() method from class A inside this class
   parent::X(); 
 }
 $obj2 = new B($obj1->a, $obj1->b);
Adil Abbasi
  • 3,161
  • 1
  • 40
  • 35
0

You will not get anything because you didn't return anything. And after reviewing your code there are some mistakes you have done.

public function abc(){
    $printthis = call_user_func(array($obj2,$method_name);
    echo $printthis;
}

You wouldn't get $obj2 and $method_name in your class A because you didn't initialize it.

i don't know how to get that data from B class into A class

by different way you can get it, you can inherit B to A or you can use getter setter for the data you want to pass. It depends on your needs.

mkj
  • 2,761
  • 5
  • 24
  • 28
sas
  • 2,563
  • 1
  • 20
  • 28
  • thank you for your answer. i'm actually weak in oop so can you give me some better example i've returned something from class B methods in my actual code. method x is called to get the properties value that is assigned in this method. now in class B i've done some code which is working fine as i've tested it in my real code. but i'm getting any idea or way to get these returned data to the method of class A. – Adnan Shawkat Nov 21 '13 at 06:25
  • I'm not getting your asking properly can you be more specific – sas Nov 21 '13 at 06:58
0

very simple for this solution , your problem is function x not called correctly, change the code: $this->x; by $this->x(); for this case

class A{
    public $a;
    public $b;
    public function __construct(){

    }
    public function x(){
        $this->a = "something";
        $this->b = "something1";
    }
    public function abc(){
        $printthis = call_user_func(array($obj2,$method_name);
        echo $printthis;
    }
}
$obj1 = new A;
$obj1->x(); //call function x correctly

class B{
    public $a;
    public $b;
    public function __construct($c,$d){
        $this->a = $c;
        $this->b = $d;
    }
    public function abc(){
        //some code here that returns something
    }
    public function abd(){
        //some code here that returns something
    }
}
$obj2 = new B($obj1->a, $obj1->b);
Makio
  • 465
  • 6
  • 15