RoyHB Posted July 5, 2011 Share Posted July 5, 2011 A main program creates instances of 2 classes. I'd like to be able to call a method in class1 from within class 2. I'd like to use a public method within the instance of class 1 that was created in the main program rather than recreating class1 within class2 first experiment that I tried the public method within class1 was not visible to class 2 class 2 will only ever be used if class 1 has been previously loaded. Can this be made to work? Thanks skeletal example: <?php $firstClass = new class1(); $secondClass = new class2(); $secondClass->doSomething(); ?> <?php class class1 { public function someFunction() { ...do some stuff } } ?> <?php class class2 { function doSomething() { $firstClass->someFunction(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241158-calling-a-class-method-from-another-class/ Share on other sites More sharing options...
AyKay47 Posted July 5, 2011 Share Posted July 5, 2011 why not make class2 extend class 1, then use parent:: Quote Link to comment https://forums.phpfreaks.com/topic/241158-calling-a-class-method-from-another-class/#findComment-1238709 Share on other sites More sharing options...
xyph Posted July 5, 2011 Share Posted July 5, 2011 You want to do something like this <?php class class2 { function doSomething( class1 $firstClass ) { $firstClass->someFunction(); } } ?> Then <?php $firstClass = new class1(); $secondClass = new class2(); $secondClass->doSomething( $firstClass ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/241158-calling-a-class-method-from-another-class/#findComment-1238710 Share on other sites More sharing options...
AyKay47 Posted July 5, 2011 Share Posted July 5, 2011 the example that xyph gave you is a method called typehinting...look here for more info Quote Link to comment https://forums.phpfreaks.com/topic/241158-calling-a-class-method-from-another-class/#findComment-1238728 Share on other sites More sharing options...
TeNDoLLA Posted July 6, 2011 Share Posted July 6, 2011 You could also just simply inheritate the class2 from the class1 to use class1's functions inside class2. class Class1 { function test1() { echo 'Function from class1<br>'; } } class Class2 extends class1 { function test2() { echo 'Fcuntion from class2<br>'; } } $obj = new Class2(); $obj->test1(); // Function from class1 $obj->test2(); // Fcuntion from class2 Quote Link to comment https://forums.phpfreaks.com/topic/241158-calling-a-class-method-from-another-class/#findComment-1238926 Share on other sites More sharing options...
ignace Posted July 6, 2011 Share Posted July 6, 2011 You could also just simply inheritate the class2 from the class1 to use class1's functions inside class2. why not make class2 extend class 1, then use parent:: We don't know what each class does since the OP did not give any concrete code. So it's best to advice composition over inheritance IMO. Quote Link to comment https://forums.phpfreaks.com/topic/241158-calling-a-class-method-from-another-class/#findComment-1238974 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.