Home » Php » php – use namespace in @param

php – use namespace in @param

Posted by: admin July 12, 2020 Leave a comment

Questions:

Is it good practice to include namespaces for classes in @param annotations? I know that phpdoc does not support namespaces, but how will other tools like phpdox or Doxygen act?

Which way is better / more common?

namespace foo\someNamespace;
use foo\someOtherNamespace\MyOtherClass;

--- with namespace ---

/**
 * @param \foo\someOtherNamespace\MyOtherClass $otherClass
 */
class myClass(MyOtherClass $otherClass)
{
    // do something
}

--- without namespace ---

/**
 * @param MyOtherClass $otherClass
 */
class myClass(MyOtherClass $otherClass)
{
    // do something
}
How to&Answers:

The namespace is a part of the complete name of the class. So if you wouldn’t add the namespace to the classname in @param you would give a wrong type.

Personally I think namespaces will become very soon the main criteria for the organization of classes in PHP. So the documentation tools will all have to use them.