method as callback function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ho.ho
Forum Newbie
Posts: 2
Joined: Thu Apr 13, 2006 5:30 pm

method as callback function

Post by ho.ho »

Hello, I want to ask you to help me with this problem:

I want to a callback function for preg_replace_callback. This callback is a class method because it need an access to some object variables.

I've found this solution how to "adress" it:
preg_replace_callback($pattern, array($this,'my_callback'),$string);

works great, but..

I change the instantion (is my english correct???) variable in my_callback:
$this->inst_var++;

while printing some debug info in my_callback, $this->inst_var changes for every pattern convenient in preg_replace_callback.

The problem is, that when i "echo" $this->inst_var after finishing preg_replace_callback, "0" is printed..


Before trying to implement THE SAME in "objects", everything was working good..

I'll be very greatful for any help
Thanks
HoHo
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think I'm going to need to see your code to be of any help.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

(assuming that the object is copied instead of referenced)

Try with

Code: Select all

array(&$this, 'your_callback');
ho.ho
Forum Newbie
Posts: 2
Joined: Thu Apr 13, 2006 5:30 pm

Post by ho.ho »

Thank you timvw, that was the problem.
Now it works!!!!!!!!!!!!!
Post Reply