JongMin Posted August 9, 2013 Share Posted August 9, 2013 Hi, Please consider the following: -------------------------------------------------------------- Cool.php -------------------------------------------------------------- <?php include_once "Common.class.php"; include_once "Test.class.php"; $common = new Common; -------------------------------------------------------------- Common.class.php -------------------------------------------------------------- <?php class Common { public blahblah; public function __construct() { blah blah } public function initSomething() { //Do something } } -------------------------------------------------------------- Test.class.php -------------------------------------------------------------- <?php class Test { public function __construct() { initSomething(); // This is the method in the Common class and I want to use it here. } } In Test.class.php I want to use the initSomething() method in the Common class and in this case how do you normally archive this? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/280979-how-to-use-a-method-across-different-php-filesclasses/ Share on other sites More sharing options...
Muddy_Funster Posted August 9, 2013 Share Posted August 9, 2013 That's not enough information to say how you would "normally" achieve this, you will need to provide the full context of what you are trying to achieve. One way would be to extend the common class, but that's situation dependant, It could just be that your class structure is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/280979-how-to-use-a-method-across-different-php-filesclasses/#findComment-1444125 Share on other sites More sharing options...
PravinS Posted August 9, 2013 Share Posted August 9, 2013 check inheritance http://php.net/manual/en/language.oop5.inheritance.php Quote Link to comment https://forums.phpfreaks.com/topic/280979-how-to-use-a-method-across-different-php-filesclasses/#findComment-1444127 Share on other sites More sharing options...
ignace Posted August 9, 2013 Share Posted August 9, 2013 (edited) Don't use inheritance. Test is not a Common. Instead pass it through the constructor or a setter. class Test { private $common; public function __construct(Common $common) { $common->initSomething(); } } Edited August 9, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/280979-how-to-use-a-method-across-different-php-filesclasses/#findComment-1444130 Share on other sites More sharing options...
Muddy_Funster Posted August 9, 2013 Share Posted August 9, 2013 ...Test is not a Common.... How can you be so sure? Quote Link to comment https://forums.phpfreaks.com/topic/280979-how-to-use-a-method-across-different-php-filesclasses/#findComment-1444131 Share on other sites More sharing options...
ignace Posted August 9, 2013 Share Posted August 9, 2013 Years of experience. Quote Link to comment https://forums.phpfreaks.com/topic/280979-how-to-use-a-method-across-different-php-filesclasses/#findComment-1444197 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.