Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get autocomplete for magic method __call - PHP Editors

we can have an autocomplete in PHP Editors for a class like:

<?php

/**
 * Class Controller
 * @property foo foo
*/
class Controller {
   public function bar() {
      $this->foo-> // autocomplete here
   }
}

class foo() {
}

but if i want an auto complete for a magic method like __call how is that possible

example below:

<?php

Class Controller {

   public function __call($function, $arguments) {
      if($function == 'foo') {
         // some logic here
      }
   }
}

Class Home extends Controller {
   public function somefunction() {
      $this-> // should have an autocomplete of foo
   }
}

any idea how could this be achieved to configure auto-complete in PHP Editors

I use PHP-Storm if there is something specific

like image 686
Guns Avatar asked May 12 '14 09:05

Guns


People also ask

What is a magic method in PHP?

PHP magic methods are special methods in a class. The magic methods override the default actions when the object performs the actions. By convention, the names of magic methods start with a double underscore ( __ ). And PHP reserves the methods whose names start with a double underscore ( __) for magic methods.

How to make the code shorter using magic method in Python?

But you can use utilize the __call () magic method to make the code shorter. The following defines the Str class that uses the __call () magic method: How it works. The $functions property store the mapping between the methods and built-in string function.

What happens when you call a method that doesn’t exist in PHP?

When you call a method on an object of the Str class and that method doesn’t exist e.g., length (), PHP will invoke the __call () method. The __call () method will raise a BadMethodCallException if the method is not supported. Otherwise, it’ll add the string to the argument list before calling the corresponding function.

What is autocomplete and how does it work?

The user is writing text in a highly structured language, such as code. There are many implementations of AutoComplete, including Visual Studio Intellisense, Google Suggest, Word Processors, and in the last few years it’s become quite ubiquitous on mobile devices.


1 Answers

you can use @method phpdoc tag to get autocomplete for magic methods

here is a code example for you:

<?php

/**
 * Class Controller
 * @method mixed foo() foo($parametersHere) explanation of the function
 */
Class Controller {

   public function __call($function, $arguments) {
      if($function == 'foo') {
         // some logic here
      }
   }
}

This should work well

like image 62
user2928120 Avatar answered Oct 20 '22 04:10

user2928120