SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict CommanderZ's Avatar
    Join Date
    Apr 2006
    Location
    Czech Republic
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array_walk using using function inside a class?

    Hello,
    How to call functions which are inside classes as a parameter of array_walk? I have tried plain "functionname" and it throws an error, that the function does not exist.

    Thanks for help

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2007
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depends on the class type:

    If the class is static:
    array_walk($array, array('self', 'walkFunction'));
    or
    array_walk($array, array('className', 'walkFunction'));

    Otherwise:
    array_walk($array, array($this, 'walkFunction'));

  3. #3
    SitePoint Addict CommanderZ's Avatar
    Join Date
    Apr 2006
    Location
    Czech Republic
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it is strange there is no word about that in neither reference nor its discussion.

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CommanderZ View Post
    Thanks, it is strange there is no word about that in neither reference nor its discussion.
    At http://www.php.net/array_walk, the function signature is:
    Code:
    bool array_walk ( array &$array, callback $funcname [, mixed $userdata] )
    At the end of that page, there is a reference to the callback type.

    There is a comment which discusses using callbacks too (though I think the person who posted it is wrong about the array(&$this, 'method') nomenclature).

    One can call a class method anonymously using the call_user_func_array() method (see the page about callbacks). This has been cleverly used to implement proxy objects and lazy loading in the WACT framework, whose main author lurks in the PHP Application Design forum under the name Selkirk. As I recall, Jason Sweat, who also contributes to the forum, has used it in his book about Design Patterns.
    Zealotry is contingent upon 100 posts and addiction 200?

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by auricle View Post
    There is a comment which discusses using callbacks too (though I think the person who posted it is wrong about the array(&$this, 'method') nomenclature).
    If you use PHP4, the comment is valid. For PHP5, you should just use Array($this, 'someMethod') as you already suggested.

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    If you use PHP4, the comment is valid.
    It's been a while since I used an ampersand! (Not missing PHP4 at all.)
    Zealotry is contingent upon 100 posts and addiction 200?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •