Call to "undefined" method Page::render() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

28th Jun 2020, 1:20 AM
2147483647
2147483647 - avatar
3 Answers
+ 3
((new Page(function (){ echo "test";}))->render)();
28th Jun 2020, 1:57 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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)();
28th Jun 2020, 6:22 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
And it worked! But why?
28th Jun 2020, 3:54 AM
2147483647
2147483647 - avatar