H
H
HunteX2014-04-15 19:52:43
PHP
HunteX, 2014-04-15 19:52:43

How to explicitly set the type of a PHP variable in PhpStorm?

Faced with an unpleasant fact, unlike C #, non-strict typing reigns in PHP. When developing, I use the PhpStorm IDE, the problem is that some functions do not return a variable of an explicit type. Here is one such function (hello Symfony2):

$server = $em->getRepository('MyBundle:Server')->findOneById($id); // $server является неизвестным типом со стороны PhpStorm

It is clear that you must explicitly specify the type. Explicit cast approach fails - PhpStorm shows syntax error:
$server = (Server)$em->getRepository('MyBundle:Server')->findOneById($id); // ошибка синтаксиса

And as a result, we have a lack of support for code completion a la intellisense (and other necessary features) for such variables that do not have a strictly defined type.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2014-04-15
@HunteX

If the problem is only code completion, use phpdoc

/** @var Server $server */
$server = $em->getRepository('MyBundle:Server')->findOneById($id);

S
Scat, 2014-04-18
@Scat

You can also set up a meta instruction for the factory
confluence.jetbrains.com/display/PhpStorm/PhpStorm...
After writing the .phpstorm.meta.php file, be sure to restart the storm (in older versions it does not work without restarting)

A
Alexander Zelenin, 2014-04-15
@zelenin

and what are you clearly leading to? to server? I didn't see this in the documentation.
But phpdoc will help in this matter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question