0

I'm having a problem when converting a string to an object. Here is the function:

public function slikepoid($dire,$id)
{
    $this->dire=$dire;
    $this->id=$id;
    $slike = $this->skupljanjeslika($this->dire);
    $slikeid = array_filter($slike, function($el) { 
        return substr( $el, 0, 2) == '$this->id-'; // Here is the problem !
    });
    return $slikeid;
}

I got this error:

Fatal error: Using $this when not in object context on line 8

I tried:

 return substr( $el, 0, 2) == ''.(string)$this->id;'-'; 

But no luck :(

2
  • In PHP 5.3, you can't use $this inside anonymous functions (closures). I think you can do that in PHP 5.4. Commented Jul 31, 2012 at 20:00
  • Yes but how to convert constructor in string? Commented Jul 31, 2012 at 20:02

1 Answer 1

5

You should be able to use a closure to accomplish this:

$slikeid = array_filter($slike, function($el) use( $id) { 
    return substr( $el, 0, 2) == $id; 
});

Now, $id should be inside the scope of the anonymous function, so you should be able to compare the element value against it.

Sign up to request clarification or add additional context in comments.

1 Comment

public function slikepoid($dire,$id) - its define closure??

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.