DEV Community

Discussion on: PHP - Elegant method call

 
jamiesonroberts profile image
Jamieson Roberts

Try the following as a replacement of line 11.

$something = Something;
$something(['dev.to', 'is', 'really', 'cool']);
Thread Thread
 
suddenlyrust profile image
Ruslan

Still getting following fatal error

PHP Fatal error: Uncaught Error: Call to undefined function Something() in /home/runner/main.php:12

What am I missing? 😖

repl.it/repls/EssentialMarvelousWe...

Thread Thread
 
jamiesonroberts profile image
Jamieson Roberts • Edited

You are missing the class instantiation (new), but I think that is my fault, I copied the wrong thing.

<?php

class Something
{
  public function __invoke($params)
  {
    var_dump('Hi there 👋. I invoked myself 😁');
  }
}

$something = new Something();
$something(['dev.to', 'is', 'really', 'cool']);

var_dump('Did the invoke work? 😕');
Thread Thread
 
suddenlyrust profile image
Ruslan

It's working now 🥳 we found the missing key to the puzzle. The invoke variant looked promising but at the end it still requires 2 lines of code.

But thank you Jamieson for showing me this variant. Did not know how to call the magic invoke method #TIL. This can be handy for sure 🤝