Jump to content

array_map warnings


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