/    Sign up×
Community /Pin to ProfileBookmark

callback functions in a class

I’ve spent a lot of time on google looking for tutorials or references on how to use callback functions. In particular, I’m trying to find out how to use a callback function within a class.

For instance, part of my class attempts to parse an xml file. To register the xml handle, I call the xml_set_element_handler() function:

xml_set_element_handler($parser, “start” , “end”);

where “start” and “end” are callback functions.

The problem is, I cannot call another function within a class without using the $this-> handle. But, this won’t work for the callback function. In other words, calling the function like $this->”start” does not work. So, how do I do this inside a class?

Again, I couldn’t find a darn thing on google. So, if you want your effective answer at the top of search engines, go at it. Thanks.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@sitehatcheryauthorDec 02.2006 — Aha, someone's stumped.

I think I may have figured it out through trial and error, though I haven't had time to test it. I didn't get any errors, but neither did I get the result I expected.

I think you can callback function in a class this way: "$this->start", rather than doing it like $this->"start". Am I going in the right direction with this?
Copy linkTweet thisAlerts:
@bokehDec 02.2006 — A method of an instantiated object is passed as an array containing an object as the element with index 0 and a method name as the element with index 1. [code=php]MyMethod($SomeInput, array($this,'MyCallbackMethod'));[/code]
Copy linkTweet thisAlerts:
@sitehatcheryauthorDec 02.2006 — In this case, it seems that the callback function is processed, the value is returned and then passed to the class. I want to define and use the callback function within the class definition.
Copy linkTweet thisAlerts:
@bokehDec 02.2006 — Can you provide a [B]simple [/B]hypothetical example (a piece of code)?
Copy linkTweet thisAlerts:
@sitehatcheryauthorDec 02.2006 — [code=php]
class Example{
$something="";

function starting($parameter){
...
$this->something=$parameter;
...
}

function ending(){
...

unset($this->something);
...
}

function parse($parser){
...
xml_set_element_handler($parser, "$this->starting" , "$this->ending");
...
}

}
[/code]


I couldn't call the callback function like $this->"starting". So, I'm trying to come up with a way to make it work in a class. I think that "$this->starting" could work. What are your thoughts on how to approach this?
Copy linkTweet thisAlerts:
@bokehDec 02.2006 — I'm not really sure what you are asking but you call a method with the following syntax. I don't see a callback here though. What did you mean callback? [code=php]xml_set_element_handler($parser, $this->starting() , $this->ending()); [/code]
Copy linkTweet thisAlerts:
@sitehatcheryauthorDec 02.2006 — I'm trying to figure that out myself. Here's is the code that I am trying to incorporate into a class:

http://www.zend.com/manual/ref.xml.php

Rather than calling loose functions, I wanted to make a class. But, for some reason, the callback functions are not recognized within the class, because I need to use the $this handle. But $this->"callback_function" does not work. "$this->callback_function" did not produce any errors, but I didn't get the output I expected either. So, either my code is wrong in another place, or I'm going about this the wrong way.

I couldn't find any resources that define callback functions or why I should use them in PHP. There are a lot of C++ references (i.e. http://www.google.com/search?hl=en&q=definition%3Acallback+function&btnG=Google+Search), but I can't seem to get a grip on how this relates to PHP. I think what I am doing is setting up a schema/framework for processing that will occur at a later time. I'd like to learn more about this as there may be some use for this down the road. I've used them with ob_start() and attempted to use it here, but that's about it.
Copy linkTweet thisAlerts:
@bokehDec 02.2006 — $this->"callback_function"[/QUOTE]That is not how you call a method. See my posts above.
Copy linkTweet thisAlerts:
@chazzyDec 02.2006 — bokeh - callback methods are very different from functions.

sitehatchery - see my comment to bokeh above. I think you see this a lot better.

From the docs on php.net, it looks like you need to define the method, and then call it via "call_user_function". note that it must be a static method, meaning it cannot modify the members. That's the general rule of thumb for callbacks in any language.

In your case, you would need to enter an array of the class name + the method name, not using $this.

Also note that based on the documentation you're referencing, the starting/ending functions you showed in your example class are not valid.

http://us3.php.net/manual/en/language.pseudo-types.php
×

Success!

Help @sitehatchery spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...