www.webdeveloper.com
Results 1 to 6 of 6

Thread: Validation issues

  1. #1
    Join Date
    Feb 2008
    Posts
    19

    Exclamation Validation issues

    Hi

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

    PHP Code:

    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
    I'm confused

    Produces the following output:

    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] => �
    )
    1

    Array
    (
    [0] => �
    )
    1


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

    Array
    (
    [0] => �
    )
    1

    Array
    (
    [0] => �
    )
    1

  2. #2
    Join Date
    Feb 2008
    Posts
    19
    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

  3. #3
    Join Date
    May 2014
    Posts
    905
    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 handler:
    array('self', 'testStatic'),

    Should be throwing an error. If it isn't somethings REALLY wrong there since the second parameter MUST be a FUNCTION.
    Java is to JavaScript as Ham is to Hamburger.

  4. #4
    Join Date
    Feb 2008
    Posts
    19
    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

  5. #5
    Join Date
    Feb 2008
    Posts
    19

    Cool

    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.

    PHP Code:
    $string preg_replace_callback('/[^a-z]/', function() use($string){
        return 
    self::toUnicode($string);
    }, 
    $string); 
    Found at the manual: http://www.php.net/manual/en/functio...mous.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/langua...s.callable.php

    Hope this helps someone.

  6. #6
    Join Date
    May 2014
    Posts
    905
    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.
    Java is to JavaScript as Ham is to Hamburger.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles