5

I have a class which includes a file in a method like below:

In class.php file:

class A {

const CONST1 = 5;

/** @var int $a */
var $a = 5;

public function call()
{
    include( 'b.php' );
}

public function do_some_magic()
{
    // magic stuff...
}

public static function static_func()
{
    // some code...
}

}

file b.php:

<?php
/** @var A $this */
/** @var A self */ // This doesn't work

$this->do_some_magic();

echo '['.$this->a.']';

self::static_func();

echo '['.self::CONST1.']';

I use PhpStorm as IDE and in b.php file if I want to go to definition of do_some_magic() or definition of a variable it will correctly go to corresponding method or variable definition in class.php file, but if I want to go to definition of constant CONST1 or to definition of static method static_func() it says "Cannot find definition to go to", so I think /** @var A self */ is not a valid notation in included file.

My question is: Is there any way in PhpDoc to tell IDE of what type self is in included file?

2
  • Why do you use include() within a class method? Commented Jul 9, 2015 at 7:25
  • I use this in particular case of a WordPress plugin to include a view which should display a form or a specific functionality. Commented Jul 9, 2015 at 9:14

1 Answer 1

3

I searched for an answer, but as it seems at the moment only way to have auto-complete in an included file you will have to make static calls using class name. Of course, this would fail if you include the file in different classes and you want self to mean that specific class where the file was included.

So b.php would look like:

<?php
/** @var A $this */
/** @var A self */ // This doesn't work

$this->do_some_magic();

echo '['.$this->a.']';

A::static_func();

echo '['.A::CONST1.']';

If however you find a better answer please let me know.

Regards,

Andy

Sign up to request clarification or add additional context in comments.

3 Comments

In PHPstorm, when something like this appears I use your notation @var ClassName $this and references work well, so I suppose this could be the real answer :-)
@ErenorPaz unfortunately, that seems only to reveal public methods and members. It's better than nothing, though.
@ErenorPaz Actually, although it doesn't suggest protected/private members, it still knows what they are. This is actually very useful. Supposing $this is class foo, which has a protected member object of class baz. Inside my included file of class foo, I can set $baz = $this->baz; (PhpStorm will not suggest $baz because it's protected). Then, if I start typing $baz->, PhpStorm will suggest all public methods and members of baz. Still, not being able to get autocompletion for non-public foo components is a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.