1

PHP returns Call to undefined method stdClass::fragmentr() for the following code:

$core = new \StdClass;

$core->fragmentr = function($frag) {
    $path = sprintf("fragments/%s.php", $frag);

    if(is_file($path)) {
        include $path;
    } else {
        header("Location: ./?p=500");
        exit;
    }
}

/* SNIP */

$core->fragmentr('header');

However, the function $core->fragmentr is clearly defined as an anonymous function assigned to a variable. Any ideas?

1 Answer 1

2

This is because your stdClass doesn't have a method called: fragmentr, but it has a property called: fragmentr so you can do something like this:

$property = $core->fragmentr;
$property("header"); 

You can't call your Closure directly as you can see, so you have to assign it to a variable and then you can call it

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

2 Comments

Is there any way to call it using $var->func() syntax besides making a class? But thanks, this is a good answer.
@Saikyr Not directly, but you could do something like this: stackoverflow.com/a/2938020/3933332

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.