PhpStorm 2017.1 Session Object Method Warning
Hi there,
This might be a PHP thing and not a PhpStorm thing? But after a lot of time on google I can't seem to find a good answer. The issue is with calling methods from session variables that are objects, both on instantiation and after a page refresh.
On instantiation I would expect to not get an error unless there's a protocol thing I'm missing?
After page refresh I still get the error as well. Somewhere I read that when you serialize an object, that the methods don't go with it, only the properties do. However, when I run the test code below it works but PhpStorm gives the following error (locations noted below) "Method echo_string not found in" (it stops here prompts for more, if I click on more it picks back up two lines later without completing the first sentence and says) "Referenced method is not found in subject class." I also read that PHP auto-serializes and auto-unserializes session objects but that it does it differently than calling the serialize() and unserialize() functions -- could this be the difference? Does the session treatment preserve methods and PhpStorm isn't recognizing that? Clearly if the methods didn't go with the object I would get PHP errors, right?
So what's not right here, the code or the warning?
Thank you for your thoughts!
<?php
class test {
public $string = 'hello from an object!';
public function echo_string($input_string = ''){
if (empty($input_string)){
echo $this->string;
}
else{
echo $input_string;
}
}
}
session_start();
echo 'Testing objects in sessions<br>';
if (!isset($_SESSION['test']['obj'])){
// set the object and put it in a session variable
$_SESSION['test']['obj'] = new test;
echo 'we just set the new object and saved it into sessions<br>';
$_SESSION['test']['obj']->echo_string(); // this generates the PhpStorm warning
}
else{
$_SESSION['test']['obj']->echo_string('This is the session var!<br>'); // this generates the PhpStorm warning
$object = $_SESSION['test']['obj'];
$object->echo_string('grumpy var!<br>'); // this generates the PhpStorm warning
$object = unserialize(serialize($_SESSION['test']['obj']));
$object->echo_string(); // this does not generate the PhpStorm warning
}
$test = new test;
$test->echo_string(); // this does not generate the PhpStorm warning
请先登录再写评论。
Hi there,
PhpStorm does not track what type particular array element is (I'm referring to $_SESSION['test']['obj']).
The "proper way" (that IDE understands .. and more clear in general) considering your code would involve intermediate variable which you can typehint with PHPDoc comment. Downside is: you are introducing intermediate variable. If you are using this variable few times later -- not a problem at all; if just once here -- waste of code lines (3 lines vs 1).
Alternatively you can create a function that 1) would provide such object and 2) would provide correct typehint. Simplified version (no proper checks etc):
Then you can use it as
Interesting, so it seems like a scope thing with PhpStorm (not knowing what's in that session var). I like your suggestions, both seem to work well, many thanks!