You are here: passing one class's function to another object « All PHP « IT news, forums, messages
passing one class's function to another object

Posted by Dominic on 05/05/06 05:14

I've written a bunch of classes in PHP5 that correspond to various data
structures on the client xhtml. Each structure is ajax enabled, and sends a
request back to the server via ajax that identifies the calling object's ID.
I use the same ID on the clientside and serverside. Using this information,
I can identify the corresponding instance on the server that was clicked on
the client and process the data using a special handler function. Here's
where my problem comes in.

Each object on the server has a variable called "handler" which holds the
function that will be used to process the ajax request. If the function is
in the global scope, I have been able to pass the function name as text to
the ajax processor object and call the function without parameters.
However, I would like to assign a function from within the class to be the
default handler if no other handler is assigned. That function never makes
it to the ajax processor object. I can't seem to pass it or reference it.

Let me try to explain with an example, edited down for the sake of brevity:
<?
function process(){ // processor function in the global scope
// grab the ajax request (a global variable)
// do something
}
class Tab {
var $handler;
function __construct(){
// generates a tab on the client that makes a request to the server via
ajax
}
function getHandler(){
if (isset($this->handler))
return $this->handler;
else
return "differentProcess"; // THIS IS THE PART THAT NEVER WORKS
// I HAVE TRIED "$mytab->differentProcess",
"$this->differentProcess", etc...
}
function setHandler($handler){
$this->handler = $handler;
}
function differentProcess(){ // processor function in the scope of this
class
// grab the ajax request (a global variable)
// do something else
}
}
class Ajax { // Ajax class to handle the request from the client (I removed
most of the non-pertinant stuff)
static function process(){
$thisid = self::$clientObject->id;
global ${$thisid};
$handlerfcn = ${$thisid}->getHandler(); // THIS GETS THE HANDLER
FUNCTION AS TEXT
$handlerfcn(); // THIS IS THE KEY FUNCTION THAT CALLS WHATEVER THE
HANDLER WAS
$responsearray = self::$response->getArray();
echo self::$json->encode($responsearray); // stringify the response and
return it to the client
}

$mytab = new Tab;
$mytab->setHandler("process"); // THIS WORKS. The Ajax class calls the right
function when a request is made.
// however the default handler within the Link class does not work.
?>
Am I missing something? Can I simply not do what I am trying to do in PHP?
Maybe there is something I can do with referenced functions?

Thanks to anyone who has made it this far in reading my problem. I look
forward to any and all responses.

Best,
Dominic

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация