March 5, 2012

parameter passing between two classes in two different files

Question by Aragorn

i want to ask how to pass parameter between two classes in two different files?
ex:

class a{
  function aa(){
    $var1 = 10;
    return $var1;
  }
}
//in different file
class b{
  function bb(){
    echo"$var1"; //how this can be achieved?
  }
} 

Answer by Starx

None of the answers here are right, since the op is asking to pass the parameter and not call it. The correct way to do it, is by creating an object of class b

class a{
  function aa(){
    $var = 10;
    $obj_b = new b(); //create an object of the class b
    $b -> bb($var); //pass the $var value to bb method of class b
  }
}

//in different file
class b{
  function bb($var){
    echo $var;  //now your values is passed here
  }
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!