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?

Carey
  • 650
  • 1
  • 4
  • 16

1 Answers1

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

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • 1
    Is there any way to call it using $var->func() syntax besides making a class? But thanks, this is a good answer. – Carey Mar 10 '15 at 14:17
  • @Saikyr Not directly, but you could do something like this: http://stackoverflow.com/a/2938020/3933332 – Rizier123 Mar 10 '15 at 14:18