GoNz0 Posted June 14, 2008 Share Posted June 14, 2008 Hey guys, Just a quick one. I have multiple classes, all invoked at the beginning of my script. One of them has to dynamically call from certain other classes during its execution. I'll give you an example of my code to date: index.php: <?php include("classes/A.php"); include("classes/callme/B.php"); include("classes/callme/C.php"); $A = new A; $B = new B; $C = new C; $something = "B"; $A->dosomething($something); ?> Class A: <?php Class A { public function dosomething($something) { print call_user_func(array(&$something,'special')); } } ?> Class B: <?php Class B { public function special() { return $this->getTheAnswer(); } public function getTheAnswer() { global $C; return $C->answer(); } } ?> Seems logical, no? I get this: Fatal error: Using $this when not in object context in /www/classes/callme/B.php on line 4 I've found a way to work around that - passing on the module name (B) to B when we call it and using call_user_func() again (it's a bit of a hackjob, but it does it). So in Class A: print call_user_func(array(&$something,'special'), $something); And in Class B: public function special($something) { return call_user_func(array(&$something, 'getTheAnswer')); That calls the function fine - hurray! However, I now get... Fatal error: Call to a member function answer() on a non-object in /www/classes/callme/B.php on line 11 So it's not recognising I'm trying to call $C->answer() (please assume the function is correct and working, I can't be bothered typing up any more psuedo code ) - I think it's assuming I'm attempting to call from within the same object or from an uninitialised class. Either that or I would have to go through the whole call_user_func() setup on my getTheAnswer() function too - this function is called by other methods and at other times, so I would not like to swap it over from $this. Also, there will be a switch in special() eventually, so it will be pulling out different functions depending on the context of the call - this would mean all functions in all possible modules called would have to use call_user_func() instead of $this =/. The best information I've been able to find so far is this PHP bug, which someone opened about the same issue. PHP devs advised $this is no longer will work in this situation as the class is called via a static method. If $this was working in a dynamic method (e.g. the class I have pulled up via call_user_func() is the current $this...), all would be working, I think.. Any suggestions guys? . Apologies if my post's a bit hard to understand - I'm having issues understanding it at the moment . Cheers, - GoNz0 Quote Link to comment https://forums.phpfreaks.com/topic/110222-working-between-dynamic-classes/ Share on other sites More sharing options...
hansford Posted June 14, 2008 Share Posted June 14, 2008 why not just make the class extend the other class and you'll have full access. Quote Link to comment https://forums.phpfreaks.com/topic/110222-working-between-dynamic-classes/#findComment-565587 Share on other sites More sharing options...
tapos Posted June 14, 2008 Share Posted June 14, 2008 where is your class C declaration? we also need to see it. BTW hansford is right Quote Link to comment https://forums.phpfreaks.com/topic/110222-working-between-dynamic-classes/#findComment-565601 Share on other sites More sharing options...
keeB Posted June 14, 2008 Share Posted June 14, 2008 I'm of the belief that if the problem is too complex to explain, you haven't boiled down exactly what you want to do yet. Asking for advice on the domain will be more fruitful than trying to assess your pseudo situation. Quote Link to comment https://forums.phpfreaks.com/topic/110222-working-between-dynamic-classes/#findComment-565668 Share on other sites More sharing options...
nloding Posted June 15, 2008 Share Posted June 15, 2008 Sounds like you need to rework your class definitions. Do they need to be separate? And why are you using a global? Them are bad. Or can be. Try to avoid them. Pass the object in as a parameter, or declare the class within the other one. Does 'C' NEED to be declared outside of 'B'? Quote Link to comment https://forums.phpfreaks.com/topic/110222-working-between-dynamic-classes/#findComment-565759 Share on other sites More sharing options...
GoNz0 Posted June 15, 2008 Author Share Posted June 15, 2008 Sounds like you need to rework your class definitions. Do they need to be separate? And why are you using a global? Them are bad. Or can be. Try to avoid them. Pass the object in as a parameter, or declare the class within the other one. Does 'C' NEED to be declared outside of 'B'? Yes, it does. It's essentially a control panel that can load different modules onto it. Each module has its own class/file. I can't really allow it to extend itself, as the main class (Class A - the page loading class) doesn't know what it will be reading from until it gets to that point. I have not been able to find another viable way to access one class via another via the variable name, which is what the situation boils down to. I'm pretty new to OOP, I'll admit. I can't see a viable way I could allow classes B and C to be created/extended within class A. The original code is almost the same as the psuedo code by the way, except the function names/variable names/class names have been changed, and the irrelivant data has been removed . Any more ideas? . Thanks for the replies so far guys! - GoNz0 Quote Link to comment https://forums.phpfreaks.com/topic/110222-working-between-dynamic-classes/#findComment-565808 Share on other sites More sharing options...
Daniel0 Posted June 15, 2008 Share Posted June 15, 2008 You're doing this: $something = "B"; $A->dosomething($something); And then: public function dosomething($something) { print call_user_func(array(&$something,'special')); } You are calling B::special() statically because you are essentially doing array('B', 'special'). You should pass an instance of B to A::dosomething() instead. Quote Link to comment https://forums.phpfreaks.com/topic/110222-working-between-dynamic-classes/#findComment-565823 Share on other sites More sharing options...
GoNz0 Posted June 15, 2008 Author Share Posted June 15, 2008 You are calling B::special() statically because you are essentially doing array('B', 'special'). You should pass an instance of B to A::dosomething() instead. So if I understand correctly, I'd do something along the lines of.. $B = new B; ... $A->dosomething($B); public function dosomething($something) { print $something->special(); } Something like that? Thanks! - GoNz0 I've just tried it out - it works just as well as the call_user_func(). However I still get the "<i>Fatal error: Call to a member function answer() on a non-object in /www/classes/callme/B.php on line 11</i>" - would this be caused by another issue maybe? =/. Quote Link to comment https://forums.phpfreaks.com/topic/110222-working-between-dynamic-classes/#findComment-565905 Share on other sites More sharing options...
Daniel0 Posted June 15, 2008 Share Posted June 15, 2008 Could you post the entire B.php file? Quote Link to comment https://forums.phpfreaks.com/topic/110222-working-between-dynamic-classes/#findComment-565906 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.