Skip to content
Advertisement

Lint dynamic return types in PHP

I would like to be able to autocomplete when using my following code. I have looked for a solution, but just can’t seem to find anything.

This is about what my models look like. In the following example I would like to be able to tab example and test

class ExampleModel extends Model {
    public function example() {
        //Do Something
    }
    public function test() {
        //Do Something
    }
}

My function to retrieve a model:

/**
* Returns an object of the requested model
* @param string $model Name of the model
* @return Model Object of the model
*/
public function getModel($model) {
    $model .= "Model";
    $path = "models/$model.php";
    if (!isset($this->modelCache[$model])) {
        if (file_exists($path)) {
            require_once $path;
        } else {
            throw new Exception("Model: $model does not exist!");
        }                
        $this->modelCache[$model] = new $model($this);
    }
    return $this->modelCache[$model];
}

This is how I call a model:

$x->getModel("Example")->example();

I am looking forward to your ideas. I am very excited to hear your ideas for this problem! I am also open to suggestions regarding the getModel function itself of course.

Advertisement

Answer

Although I would still like to know how to achieve this without changing the process itself, but I have solved this problem by simply using static classes and namespaces. The linting is great like that.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement