Jump to content

[SOLVED] Can I use a class' method as a callback function for functions like array_walk


RussellReal

Recommended Posts

like.. for example:

 

<?php
class TestCallback {
  private $variable;
  private function handleWalk($theValue) {
    echo "<h1><b>{$this->variable}</b></h1><br />";
    echo $theValue.'<br />';
  }
  public function goTest() {
    $this->variable = 'TEST!!!';
    $abc = array('lol','omg');
    array_walk($abc,'$this->handleWalk');
  }
}
$t = new TestCallBack();
$t->goTest();
?>

 

NOW I know that won't work since, when the callback is triggered.. it will not know what $this is, but if I did like TestCallBack::handleWalk instead of $this->handleWalk it will most likely work..

 

I want callback functions to be able to be a method within the $this object inside my class.. if I'm making any sence..

 

in other words.. I want to keep it all within the class, instead of another class to extend the main class in order to do this

Link to comment
Share on other sites

ok.. thank you so much, that made my life alot easier

 

I have been helping here for like 2 months or so or a month I'm not exactly sure..

and so like I was on now and was like.. "Hey why not ask and see if its possible", so I asked.. lol :)

 

and I've been puzzled over this since like a year ago already.. so, thank you very much for your reply :D

Link to comment
Share on other sites

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.