0

How to retrieve method of a class in anonymous function? Does another opportunity exist to solve this?

Task: I need to upload a image from remote path and change it to local path.

Code:

    $pattern = '/<img src=(.*?jpg|gif|png).*?>/m';

    $uploadImage = function($image)
    {
        $this->uploadPictures();
    };

    function image_replace($matches) use ($uploadImage)
    {
        // как обычно: $matches[0] -  полное вхождение шаблона
        // $matches[1] - вхождение первой подмаски,
        // заключенной в круглые скобки, и так далее...
        $uploadImage($matches[1]);

        return $matches[1].($matches[2]+1);
    }

    preg_replace_callback(
        $pattern,
        "image_replace",
        $text);
4
  • Which version of PHP do you using? Commented Jul 5, 2013 at 9:36
  • can you translate the comment part in your code? Commented Jul 5, 2013 at 9:37
  • 2
    @sinaneker Comment is not relevant to the problem. It just explains what $match contains. Commented Jul 5, 2013 at 9:40
  • @korvinko It seems that $this is used out of the scope. Can you update how that class looks? Commented Jul 5, 2013 at 9:41

3 Answers 3

1
$pattern = '/<img src=(.*?jpg|gif|png).*?>/m';

$uploadImage = function ($image) {
    $this->uploadPictures();
};

$image_replace = function ($matches) use ($uploadImage) {
    $uploadImage($matches[1]);
    return $matches[1].($matches[2]+1);
};

preg_replace_callback($pattern, $image_replace, $text);

or

$pattern = '/<img src=(.*?jpg|gif|png).*?>/m';

$image_replace = function ($matches) {
    $this->uploadPictures($matches[1]);
    return $matches[1].($matches[2]+1);
};

preg_replace_callback($pattern, $image_replace, $text);

or

$pattern = '/<img src=(.*?jpg|gif|png).*?>/m';

preg_replace_callback($pattern, function ($matches) {
    $this->uploadPictures($matches[1]);
    return $matches[1].($matches[2]+1);
}, $text);
Sign up to request clarification or add additional context in comments.

Comments

0

You can pass the class object to the anonymous function

$uploadImage = function($image, $this)
{
    $this->uploadPictures();
};

Comments

0

From PHP 5.4 $this can be used in anonymous functions (see changelog here).

Thus, code such as this:

<?php
class A
{
    public function test()
    {
        $f = function()
        {
            return $this->callback();
        };
        return $f();
    }

    public function callback()
    {
        return 'test';
    }
}

$a = new A();
echo $a->test();

is perfectly valid from 5.4 on.

In PHP 5.3 you could achieve the same thing using use operator:

public function test()
{
    $_this = &$this;
    $f = function() use (&$_this)
    {
        return $_this->callback();
    };
    return $f();
}

Prior to PHP 5.3, you could avoid using use keyword through usage of function arguments:

public function test()
{
    $f = function($_this)
    {
        return $_this->callback();
    };
    return $f($this);
}

Finally, when it comes to static methods, you could also make use of __CLASS__ constant.

public function test()
{
    $f = function()
    {
        return call_user_func(array(__CLASS__, 'callback'));
    };
    return $f();
}

...although it's quite ugly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.