jamesxg1 Posted August 15, 2010 Share Posted August 15, 2010 Hiya peeps! I was wondering how I could use a function from one class in another EG. class_file_one.php class a { function a() { return true; } } class_file_two.php class b { function b() { if($this->a()) { return 'it works'; } } } The classes are in completely different pages. Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/210770-how-do-i-use-a-function-from-another-class/ Share on other sites More sharing options...
jamesxg1 Posted August 15, 2010 Author Share Posted August 15, 2010 I'm sure in some way you have to use the extends but I'm not sure how LOL! Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/210770-how-do-i-use-a-function-from-another-class/#findComment-1099482 Share on other sites More sharing options...
ignace Posted August 15, 2010 Share Posted August 15, 2010 It depends if both are related then you could do: require 'a.php'; class b extends a If they are not then you must ask yourself will a exist outside of b? Yes then require 'a.php'; class b { function __construct(a $a) otherwise: require 'a.php'; class b { function __construct() { $this->a = new a(); } } Quote Link to comment https://forums.phpfreaks.com/topic/210770-how-do-i-use-a-function-from-another-class/#findComment-1099488 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.