play_ Posted October 28, 2008 Share Posted October 28, 2008 Ok. I know you can pass the object of a class as an argument. Example: class A { function test() { echo "This is TEST from class A"; } } class B { function __construct( $obj ) { $this->a = $obj; } function test() { $this->a->test(); } } Then you could do: $a = new A(); $b = new B($a); Ok so that's one way i know of. I also thought that you could make a method static, and do this: (assuming class A's test is 'static') class B { function test() { A::test(); } } But that is not working. I'd like to know all possible ways of accomplishing this. Any hints are appreciated. thanks Quote Link to comment https://forums.phpfreaks.com/topic/130440-ways-of-calling-a-classs-method-inside-another-class/ Share on other sites More sharing options...
wildteen88 Posted October 28, 2008 Share Posted October 28, 2008 Using class B { function test() { A::test(); } } Should work to call a method from an anther class without having to create a new instance. Quote Link to comment https://forums.phpfreaks.com/topic/130440-ways-of-calling-a-classs-method-inside-another-class/#findComment-676823 Share on other sites More sharing options...
ragtek Posted February 22, 2009 Share Posted February 22, 2009 hi And what if i want to use a Class inside a Class? example: class vbtwitter{ /** * Twitter object * * @var twitter object */ private $twitterconnection; function __construct() { } function init() { global $vbulletin; $this->twitterconnection = new Twitter($vbulletin->userinfo['twitter_username'], $vbulletin->userinfo['twitter_password'], 'vbtwitter'); } public function sendthread(&$threadid) { // here now call some methods from $this->twitterconnection // this->twitterconnection->methode.... } Quote Link to comment https://forums.phpfreaks.com/topic/130440-ways-of-calling-a-classs-method-inside-another-class/#findComment-768445 Share on other sites More sharing options...
Julius Posted November 6, 2011 Share Posted November 6, 2011 Isn't there any other way to do this? Did codeigniter do this the same way? Quote Link to comment https://forums.phpfreaks.com/topic/130440-ways-of-calling-a-classs-method-inside-another-class/#findComment-1285584 Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 Class inside a class is fine. There's also inheritance. All of which are fine ways to have objects interact with each other. Your issue should work fine. <?php class a { public static function func() { return 'foobar'; } } class b { public function method() { return a::func(); } } $obj = new b; echo $obj->method(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/130440-ways-of-calling-a-classs-method-inside-another-class/#findComment-1285597 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.