I have this code:
$sSomeClass = 'Database';
$pdo = $sSomeClass::getInstance();
Is there any way how to typehint in PHPDoc, that variable "$sSomeClass" is a class name, so IDE can see a reference to that class?
Well...
<?php
/**
*
* @var Database $sSomeClass
*/
But I assume $sSomeClass is not a straight assignment like shown? In that case... possibly? IDE wouldn't be able to determine it, might be able to scope the hints inside curly braces. Will depend on IDE.
if ($foo == 'bar') {
/**
*
* @var Bar $sSomeClass
*/
$sSomeClass = 'Bar';
} else {
/**
*
* @var Foo $sSomeClass
*/
$sSomeClass = 'Foo';
}
I'll add blocks like that C style comment when coding if struggling to remember methods and then simply remove it afterward.
In Eclipse you can do this (I don't know about other IDE's):
/* @var $sSomeClass SomeClass */
$sSomeClass = 'Database';
$pdo = $sSomeClass::getInstance();
Which works great for local variables, such as looping over an array of objects.
/* @var $someClass SomeClass */
foreach($objects as $someClass){
$someClass->getSomething();
}
That is the typical way I use it, at least.
If you plan to use the same variable for multiple classes, a way to do that would be to make an interface for them. That is if they are interchangeable and then use the interface instead of the class name. It's not clear in the question on exactly what you need.
interfaces can't have static methodsSense when Example. They work fine for me... Obviously you have to type hint it, but you don't have to conditionally type hint it. If a class is interchangeable, it should have an interface. This was more a reference to the accepted answer.