/    Sign up×
Community /Pin to ProfileBookmark

Validation issues

Hi

Anyone know what is up with callbacks? They seem to change results depending on type i.e. closure or method call:

[code=php]

class callbackProblems
{
public function __construct()
{
echo “<!DOCTYPE html>n”;
echo “<head>n<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>n”;
echo “</head>n”;
echo “<body>n”;

echo “<br>Simple test to see the difference in a callback being a closure, static and non-static<br>n”;

$c = “£”;

echo “We start with an annoying character: {$c} – the UK pound symbol<br>n”;

echo “<br>The preg_replace_callback function as a closure<br>n”;

$cWithCallback_closure = preg_replace_callback(‘/[^a-z]/’, function($c){
echo $c[0];
}, $c);

echo $cWithCallback_closure . “n”;

echo “<br>Now for the preg_replace_callback function as a static method call<br>n”;

$cWithCallback_static = preg_replace_callback(‘/[^a-z]/’, array(‘self’, ‘testStatic’), $c);

echo $cWithCallback_static . “n”;

echo “<br>Now for the preg_replace_callback function as a non-static method call<br>n”;

$cWithCallback_nonstatic = preg_replace_callback(‘/[^a-z]/’, array($this, ‘testNonStatic’), $c);

echo $cWithCallback_nonstatic . “n”;

echo “</body>n”;
echo “</html>n”;
die();
}

public static function testStatic($s)
{
echo “<pre>”, print_r($s), “</pre>”;
}

public function testNonStatic($s)
{
echo “<pre>”, print_r($s), “</pre>”;
}

}

$test = new callbackProblems;

[/code]

I’m confused ?:eek:

Produces the following output:

[QUOTE]

Simple test to see the difference in a callback being a closure, static and non-static
We start with an annoying character: £ – the UK pound symbol

The preg_replace_callback function as a closure
£
Now for the preg_replace_callback function as a static method call

Array
(
[0] => &#65533;
)
1

Array
(
[0] => &#65533;
)
1

Now for the preg_replace_callback function as a non-static method call

Array
(
[0] => &#65533;
)
1

Array
(
[0] => &#65533;
)
1

[/QUOTE]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@DAL_authorJun 03.2014 — FYI this was originally going to be a validation type question but during my writing a description of the problem I realised the above issue was at the core thus addressed this instead. Unfortunately, I forgot to rename my thread :rolleyes:
Copy linkTweet thisAlerts:
@deathshadowJun 04.2014 — I'm not sure what you are expecting that to do -- passing an array as the handler should be throwing an error, not plodding on like you seem to be claiming.

Though it would be a lot easier to figure out what you are trying to do if you didn't use 20 echo to do the job of three, got rid of the double quotes and C style newlines, etc, etc...

Really though, passing this as [i]handler[/i]:

array('self', 'testStatic'),

Should be throwing an error. If it isn't somethings REALLY wrong there since the second parameter MUST be a FUNCTION.
Copy linkTweet thisAlerts:
@DAL_authorJun 04.2014 — Thanks deathshadow

Sorry for all the echo's, I originally used them to see what was going on as I extended my test. I then just put more explanation in there to help anyone to understand what I wanted to achieve, yes it is rather an eye sore (my normal code doesn't look like this ? )

The array passed was something that seemed right from research. I originally jus had it as 'self::staticTest' which does the same. I'm sure the array thing was suggested by the manual but I'm on the iPhone at the moment so I'll check later.

To sum up my issue. The £ sign is fine in a closure but passed to a named method the £ sign changes for some reason into an unknown character and I don't know why?

Cheers
Copy linkTweet thisAlerts:
@DAL_authorJun 07.2014 — If anyone is interested, I seem to have solved the issue:

When calling a closure I would use the 'use' keyword on the callback/anonymous/closure function.

[code=php]
$string = preg_replace_callback('/[^a-z]/', function() use($string){
return self::toUnicode($string);
}, $string);
[/code]


Found at the manual: http://www.php.net/manual/en/functions.anonymous.php#99287

I see references have been removed as of PHP 5.4.0? I'm not sure why but then again it's got to be the first time I've tried using them. Regardless, the above code seems to produce the right results based solely on the use($string) even though the example illustrates that use(&$string) would have been better.

For the record deathshadow the array type call for closures was illustrated here. I haven't looked at this in great depth:

http://www.php.net//manual/en/language.types.callable.php

Hope this helps someone.
Copy linkTweet thisAlerts:
@deathshadowJun 07.2014 — Hmm... new in 5.3 -- missed that one. Can pass an array of callbacks? That's actually kinda cool. But then they are adding a lot of cool stuff from 5.3 onwards. See 5.4's "you don't have to say Array anymore".

As to references going, there's a lot of stuff that's fading away VERY soon; mostly just removing that there's WAY too many different ways of doing the same task.
×

Success!

Help @DAL_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...