Jump to content

array_map warnings


william@960design.com

Recommended Posts

Howdy, this one is killing me!  Hope some of the genius here will rub off on me.

 

Very simple example of what is happening:

 

Base.php

class Base_Model    //class autoloaded
{
  protected function scrub($value)
  {
    return trim(htmlspecialchars($value, ENT_QUOTES, "UTF-8"));
  }
}

 

Foo.php

class Foo_Model extends Base_Model
{
  if($_POST)
  {
      $_POST = array_map('parent::scrub', $_POST);
  }
}

 

This works perfectly on the workstation (MAMP) server, but generates a warning ( Warning: array_map() [function.array-map]: The first argument, 'parent::scrub', should be either NULL or a valid callback in... ) when uploaded to the production server.  Both are running the exact same version of php.

 

Any ideas would be appreciated on getting rid of the error, besides turning off warnings ( haha ).

 

William

Link to comment
https://forums.phpfreaks.com/topic/245160-array_map-warnings/
Share on other sites

http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback

 

I doubt they're the exact same version

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');

 

and

 

// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
    public static function who() {
        echo "A\n";
    }
}

class B extends A {
    public static function who() {
        echo "B\n";
    }
}

call_user_func(array('B', 'parent::who')); // A

 

Also

levi at alliancesoftware dot com dot au 08-Feb-2007 10:44

Parent methods for callbacks should be called 'parent::method', so if you wish to call a non-static parent method via a callback, you should use a callback of

<?

// always works

$callback = array($this, 'parent::method')

 

// works but gives an error in PHP5 with E_STRICT if the parent method is not static

$callback array('parent', 'method');

?>

Link to comment
https://forums.phpfreaks.com/topic/245160-array_map-warnings/#findComment-1259243
Share on other sites

@Alex:  pure genius ( i think i got a little on me, thanks )

 

I had to mod it slightly:

$_POST = array_map(array($this, 'Base_Model::scrub'), $_POST);

 

Could that be because of the way the mvc framework autoloads/includes class files?  I dislike using the Base_Model and would rather use parent.

 

@xyph:  excellent information, I'll try those ideas

 

@webstyles:  please correct my thinking here,  $_POST is only created after the form has been submitted, the $_POST gets pushed to whatever file.php you wish, in this case it is getting pushed to the same file.  The second time ( and third, forth, fifth, ect ) the file loads the if($_POST) would be true, but not the first time.

Link to comment
https://forums.phpfreaks.com/topic/245160-array_map-warnings/#findComment-1259405
Share on other sites

@xyph:

try this one mate

<h1>Post Test</h1>
<form name=posttest action='' onsubmit=submit.disabled=true; method=post>
<ul>
	<li>
	  <label class='' for=field>Field Label </label>
	  <input type=text name=field value='' title='' maxlength=30 >
	</li>
</ul>
  <input type=submit  name=submit  onclick=this.value='Submitting…'  value='Test' >
</form>

<?php
if($_POST)
{
echo "xyph was right";
}
else
{
echo "xyph was wrong";
}
?>

 

Not really sure why you threw the is_array($_POST) in the mix as it's only testing if $_POST is an array, which isn't the same thing I posted above.

The second one !empty($_POST) is almost exactly the same, except I saved a couple nano seconds by removing the negative test from the empty() method call and just tested for $_POST only.  Thanks for the explanation though.

 

Try my code above and see what it does for you.

 

Thanks guys, I'm marking this as fixed.

 

William

Link to comment
https://forums.phpfreaks.com/topic/245160-array_map-warnings/#findComment-1259661
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.