Jump to content

Recommended Posts

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 :P) - 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 :P.

 

 

Cheers,

 

- GoNz0

Link to comment
https://forums.phpfreaks.com/topic/110222-working-between-dynamic-classes/
Share on other sites

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'?

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? :P.

 

 

Thanks for the replies so far guys!

 

 

- GoNz0

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.

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? =/.

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.