Jonob Posted January 12, 2009 Share Posted January 12, 2009 New to php, but learning as I go. Lets say I have a php file called user.php class user { function first(); { Some code here return $result; } function second(); { //I want to refer to function first $test = $this -> first() return $test } } OK cool, so if I want to refer to another function in the same class, then I use $this -> Now, what happens if I have a second php file called (for example) company.php, and inside that file I have a class called company, and inside that class a function called get_company(). I want to use get_company() in user.php, so at the top of user.php I put require_once("company.php") Lets say I want to use the get_company function in the second() function. I've tried to reference it as $test = $this -> get_company() but it throws an error. I guess my question is: how do I reference a function in a different class? Quote Link to comment Share on other sites More sharing options...
beyzad Posted January 12, 2009 Share Posted January 12, 2009 I think you mean this: $temp = new company(); $temp -> get_company() Quote Link to comment Share on other sites More sharing options...
trq Posted January 12, 2009 Share Posted January 12, 2009 <?php class a { function boo() { echo "hello from a"; } } class b { private a; function __construct() { $this->a = new a; } function boo() { $this->a->boo(); echo "hello from b"; } } $b = new b; $b->boo(); ?> Quote Link to comment Share on other sites More sharing options...
Jonob Posted January 12, 2009 Author Share Posted January 12, 2009 Thanks guys, works perfectly On the same topic, assuming the following: -I have one class in a php file -I have many functions in that class -I need to reference other classes from other php files in only ONE of those functions Is it considered OK coding practise to put the require_once("filename.php") code inside that single function? Or should it be just after the opening <?php tag? Quote Link to comment Share on other sites More sharing options...
trq Posted January 12, 2009 Share Posted January 12, 2009 Doesn't really matter. The class will end up within the global scope anyway. Quote Link to comment Share on other sites More sharing options...
Jonob Posted January 12, 2009 Author Share Posted January 12, 2009 Thanks for the help. Much appreciated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.