0
Call to "undefined" method Page::render()
I'm building a web app for my LAN. I have a class to manage pages and its constructor takes a closure as an argument and assigns it to a property. var_dump shows everything as expected but when I call (new Page(function { echo "test"; })->render() it gives me the error "Call to undefined method Page::render()". Class code: https://code.sololearn.com/woQCeVlz7D2z/?ref=app
3 Answers
+ 3
((new Page(function (){ echo "test";}))->render)();
+ 3
Good question. Lol
I'm not a PHP expert, but it has something to do with how the member variable that is pointing to a function (anonymous or otherwise) is resolved.
BTW if you wanted to pass an actual function and or use variables instead of anonymous objects etc you can do so like.
function f() {
echo "function";
}
$page = new Page('f');
($page->render)();
+ 1
And it worked! But why?