1

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?

7
  • Why is it a variable if it's always the same class? And if it's not always the same class, how is the editor supposed to know which class it is in any particular use? Commented Oct 18, 2018 at 21:41
  • My suggestion, is to make an interface for the classes and use that as the type hint. That is the proper way to do it. Commented Oct 18, 2018 at 21:43
  • @Barmar, it's just simple example. In real scenario, it is not the same class, but some subclass of Database class. IDE doesn't need to know what concrete subclass is it, just the parent (Database) class. Commented Oct 19, 2018 at 8:20
  • @ArtisticPhoenix, that doesn't solve anything. I would still need to typehint that $sSomeClass is string containg that interface. Besides, interfaces can't have static methods. Commented Oct 19, 2018 at 8:23
  • @user10099 - interfaces can't have static methods Sense 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. Commented Oct 19, 2018 at 8:27

2 Answers 2

2

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.

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

1 Comment

Thanks, yeah, that works. But it's kinda hacky, because IDE now thinks, that $sSomeClass is an instance of that class, not the string containing that class. I thought there will be some special treating for dynamic class names. Anyway it's sufficient for me, I just need to know, who is using the class method for some future refactoring ...
0

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.

Comments

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.