How to typehint context?
I have a closure like this:
$callback = function() {
var_dump($this->title);
};
I am using Closure::call (http://php.net/manual/en/closure.call.php) for switching to another context inside the closure.
However, PHPStorm is giving me "Field 'title' not found in __anonymous@260".
When I add a @var DocBloc:
$callback = function() {
/** @var $this AudioConversion */
var_dump($this->title);
};
Then notice becomes a warning "Member has protected access" (indeed, cause it's a protected property).
Can I somehow tell PHPStorm inside the closure that it's going to be used inside another class, and that it will assume its context?
请先登录再写评论。
What version are you using? This doesn't have this error wording in latest versions (2017.2.4 or EAP build).
Latest, just checked. 2017.2 (and I tried updating just now)
Anyone?
yo?
Hi there,
Would be good if you could provide some simple standalone but complete example that can be copy-pasted into IDE to test locally (just to avoid any misunderstanding/difference in code)?
But in general: it works fine when using such PHPDoc in outside of class context (e.g. views) thanks to https://youtrack.jetbrains.com/issue/WI-5076 .. but fails if used inside the class, be it anonymous function or just class method (as from IDE point of view you may be re-declaring class itself, which may lead to IDE screwing up the rest of checks).
I may suggest to file a Feature Request ticket to the Issue Tracker specifying that it's about closure/lambda context -- I'm not sure how IDE treats such stuff here and what the possible complications are.
I've found https://youtrack.jetbrains.com/issue/WI-18308 .. but that's different story as it was about public methods.
This one may be relevant though (see the last sentence in last comment): https://youtrack.jetbrains.com/issue/WI-17165#comment=27-2091679
P.S. It's a community forums .. JB staff may not be checking it for new messages on every-few-hours-or-so basis.
Hey Andriy, here's a simple repro:
class Foo {
private $bar = 'baz';
/**
* Executes a closure in $this context and returns whatever the closure returns.
*
* @param \Closure $closure
* @return mixed
*/
public function callClosureInThisContext(\Closure $closure) {
return $closure->call($this);
}
}
class Closures {
/**
* @return \Closure
*/
public function getClosureForFoo() : \Closure {
return function () {
// how do I tell my IDE that in this context $this is actually class Foo,
// and not the class Closures?
print $this->bar;
};
}
}
$foo = new Foo();
$closures = new Closures();
$foo->callClosureInThisContext($closures->getClosureForFoo()); // prints "baz"
Thanks for the code.
I may only suggest the same again: file a Feature Request ticket to the Issue Tracker specifying that it's about closure/lambda context -- you have good and working code sample (if you know some rather big open source projects that use this -- list them -- would add some points for "better implement it sooner")
Right now typehinting $this will not help as IDE will still show the error. In order to get rid of it IDE must suppress it here (not show it at all) but for that it must be aware of such usage scenario (like it does when typehinting $this outside of the class -- e.g. view files).
Alright, I'll do that, thank you.