Posted a few days ago trying to get some clarity in how to use objects. Now I have some, but not enough ...
Simple setup (logicaly), two objects: Mysql and User. The mysql object has a methos, query, that, you guessed it, querys a mysql database. The User object has a function to list all currently registerd users.
Now, the calling order is as follows:
$user->list() ... In the method 'list' is an internal function called format(). The format function takes a post from the db and formates it nicely and prints it.
The user->list function calls mysql->query() with two variables. The first is the actual query to be exec, the other is a refrence to list:s internal function format.
The mysql->query method calls up a query and for each row it uses the refrenced function from user->list (format()) to output (or otherwice handel the information)...
Now that was the theory. I hav a stripped down version of the code just to get the calling-a-method-from-another-object-code working. It's not however and I cant seam to find ANY information on how to call the damn method from another object.
This I have tryed:
//$this is the object 'list'
//$mtest is the 'mysql' object
//$this->Queryfunc holds the string "mtest->hello" (should work)
//at least it works with normal functions...
$this->Queryfunc("Refrence");
$mtest->hello("Direct");
call_user_func(array(&$mtest, "hello"),"Call_User_Func");
call_user_method("hello",&$mtest,"Call_User_Method");
None of this works from within the object .. All calls work from outside ... I'm just so stuck. Sure I can think of several ways to solve the problem using other solutions, but I'm damned if I have to give up on this one ;o)
Bottomline: It should work, but somewere i fucked up. In the theory or in the code I dont know ....
Code in full:
<?php
class test
{
function hello($text)
{
echo $text;
return 0;
}
}
class user
{
var $Queryfunc;
var $Printfunc;
function printlist()
{
function out()
{
echo "Hello from out...";
}
$this->Printfunc = &out();
echo ("<br><br>Hello from printlist...<br>Queryfunc are: ".$this->Queryfunc."!");
$this->Queryfunc("Refrence");
$mtest->hello("Direct");
call_user_func(array(&$mtest, "hello"),"Call_User_Func");
call_user_method("hello",&$mtest,"Call_User_Method");
}
}
$bar = new user;
$foo = "mtest"; //foo innehÄller ett namn X
${$foo} = new test; //ett nytt objekt skapas med namnet X
$bar->Queryfunc = $foo."->hello";
echo ("<br><br>Queryfunc: ".$bar->Queryfunc."<br>");
$bar->printlist();
?>
The result of calling the code can be found @: http://plikt.sverok.net/class_test-php .. Sorry about the messy code. To many rewrites ...