programming.name Posted December 2, 2012 Share Posted December 2, 2012 (edited) Hi, Is there an acceptable way to call a method in a class from another class? I have 3 files and 2 classes as below: a.php class a { function func_a() { b::func_b //it is a place where I want to call func_b() and this give me an error } } b.php class b { function func_b() { echo 'I got in!'; } } c.php <?php include("a.php")// or require_once include("b.php")// or require_once $a = new a; $b = new b; ?> <html> <head> </head> <body> <?php $a->func_a(); //I got in! to the display ?> </body> </html> I tried: b::func_b in func_a in class a, it gives the following error: Strict standards: Non-static method b::func_b() should not be called statically, assuming $this from incompatible context in a.class.php on line 12 I can hide thie error using: error_reporting(E_ALL ^ E_STRICT); This is not what I want; I want some "clean code". Is there a way to achieve this? Thanks in advance. Edited December 2, 2012 by programming.name Quote Link to comment https://forums.phpfreaks.com/topic/271475-calling-a-method-in-a-class-from-another-class/ Share on other sites More sharing options...
ignace Posted December 2, 2012 Share Posted December 2, 2012 (edited) class AnObject { private $anotherObject; public function __construct(AnotherObject $B) { $this->setAnotherObject($B); } public function setAnotherObject(AnotherObject $B) { $this->anotherObject = $B; } public function showAnotherObject() { $this->anotherObject->show(); } } class AnotherObject { public function show() { echo 'I got in!'; } } $a = new AnObject(new AnotherObject); $a->showAnotherObject(); Edited December 2, 2012 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/271475-calling-a-method-in-a-class-from-another-class/#findComment-1396882 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.