Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have type hinting in PHP that specifies variable scope inside of a template? (specifically PhpStorm)

Tags:

php

phpstorm

I'm looking for a doc comment that would define the scope/context of the current php template. (similar to @var)

Example View Class:

<?php

class ExampleView {

    protected $pageTitle;

    public function __construct($title) {
        $this->pageTitle = $title;
    }

    public function render() {
        require_once 'template.php';
    }

}

--

<?php
// template.php 

/** @var $this ExampleView */
echo $this->pageTitle;

PHPStorm gives an inspection error because the access on $pageTitle is protected.

enter image description here

Is there a hint to give scope? Something like:

<?php
// template.php 
/** @scope ExampleView */ // <---????

/** @var $this ExampleView */
echo $this->pageTitle;
like image 521
Lance Rushing Avatar asked Jun 21 '12 21:06

Lance Rushing


1 Answers

Unfortunately you cannot -- there is no such tag (either in general or specific to PhpStorm).

Let's hope they (JetBrains devs) do something about it: http://youtrack.jetbrains.com/issue/WI-11022 -- vote/comment/etc and if it will get much more votes we may see it implemented soon (right now that "soon" is far away).

like image 162
LazyOne Avatar answered Nov 20 '22 09:11

LazyOne