Jump to content

Recommended Posts

Hello

I want to walk an array and handle every element I encounter with a function.
There is a problem when I use [url=http://fr.php.net/array-walk-recursive][b]array-walk-recursive[/b]
[/url] and objects.

The code works fine like it is on the php manual site but doesn't when I use Objects.
Can someone help me and tell what I am doeing wrong.

I hope it isn't a problem when I post the code here. If it does, I apoligize.

the Error
----------
Warning: array_walk_recursive() [function.array-walk-recursive]:
[!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]Unable to call test_print() - function does not exist in[!--colorc--][/span][!--/colorc--]
C:\Program Files\xampp\htdocs\www\arrayrunner.php on line 11 (and every line were rray-walk-recursive is called)

The code
-----------
<?php
$anObject = new ClassName();

[b]array_walk_recursive[/b]($anObject->getFruits(), 'test_print');
[!--coloro:#C0C0C0--][span style=\"color:#C0C0C0\"][!--/coloro--]//or[!--colorc--][/span][!--/colorc--]
[b]array_walk_recursive[/b]($anObject->getFruits(), '$anObject->test_print');
[!--coloro:#C0C0C0--][span style=\"color:#C0C0C0\"][!--/coloro--]//or...the same but in alternative way (see class)[!--colorc--][/span][!--/colorc--]
[b]$anObject->arrWalker[/b]($anObject->getFruits());
?>

<?php
//Class description---------------------------------------------
class ClassName {
//Class variables
private $sweet = array();
private $fruits = array();

//Constructor
public function __construct(){
$this->sweet = array('a' => 'apple', 'b' => 'banana');
$this->fruits = array('sweet' => $sweet, 'sour' => 'lemon');
}

//public methods
public function [!--coloro:#3366FF--][span style=\"color:#3366FF\"][!--/coloro--][b]test_print[/b][!--colorc--][/span][!--/colorc--]($item, $key){
echo "$key holds $item\n<br>";
}

public function [b]arrWalker[/b]($arr){
[!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]/*I get errors that he doesn't know test_print*/[!--colorc--][/span][!--/colorc--]
array_walk_recursive($arr, '$this->test_print');
//or
array_walk_recursive($arr, 'test_print');
}
}
?>
Link to comment
https://forums.phpfreaks.com/topic/5912-object-oriented-and-array_walk_recursive/
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.