I'm using NetBeans 8.0.1. I've also used this article to try and solve my problem.
PHPDoc allows us to specify the names and types of global variables. However, I'm not sure if I'm doing something wrong or if I've run into a limitation of PHPDoc.
My problem is the following. I have a PHP file called Constants.php which contains a global variable called $DATABASE, which is instantiated to an instance of my Database class (stored in Database.php). The Database class is located in the Core
namespace (i.e. \Core\Database
). I use autoloading for all my classes.
Basically, I want NetBeans to show autocomplete-hints for all functions/properties in the Database class as it would if I declared $DATABASE = new Database();
. In that scenario typing $DATABASE->
would show a list of functions I can use.
However, I can't seem to get this working right.
When I document my global $DATABASE like so:
File: Constants.php
/**
* The instance of the database module.
* @global \Core\Database $DATABASE
* @name $DATABASE <-I tried both with and without the name tag
*/
$DATABASE;
and my function like so (assume the function is wrapped in class MyClass):
File: MyClass.php
/**
* My function description.
* @global \Core\Database The database instance.
*/
public function MyFunction()
{
global $DATABASE;
$DATABASE->Connect();
}
In the example as given above, when I run autocomplete (ctrl+space) on the $DATABASE->Connect();
line it tells me that the $DATABASE variable refers to ? $DATABASE MyClass.php
('type' '$name' 'class'), which is the wrong class, and more importantly, misses the type. And while the function call works, the autocomplete and parameter hinting don't, which removes a big advantage of using PHPDoc.
On the other hand, if I change the function documentation to:
/**
* My function description.
* @global \Core\Database $DATABASE The database instance.
*/
public function MyFunction()
{
global $DATABASE;
$DATABASE->Connect();
}
it tells me that the $DATABASE variable refers to Database $DATABASE MyClass.php
, which is still the wrong class, but at least contains the type. At this point autocomplete works.
My issue is that according to the PHPDoc documentation (and the thread I linked above), that usage is wrong. Also, it should tell me it found $DATABASE in Constants.php.
This all leads me to the problem I can't work around: I also want the autocomplete when I use the $DATABASE global outside of a class, and outside of Constants.php. I currently don't see any way to get it to display autocomplete when using it like this:
File: MyOtherFile.php
namespace Core
{
$DATABASE->Connect();
}
Any help would be greatly appreciated. It may just be I'm trying to push PHPDoc to its limits, it may be PHPDoc doesn't support hinting on custom classes (doesn't seem likely seeing as that I can get it to work sometimes), or I might just be doing something wrong.
Any help here would be extremely appreciated.