NetBeans auto-completion does not show class methods when using the Singleton design pattern

WebTechRiser.com > Joomla 3.0 > NetBeans auto-completion does not show class methods when using the Singleton design pattern

It was a very weird problem that ate up my whole day while writing a custom component in Joomla! 3.2.2. I found a solution on a page of StackOverflow.

I was using NetBeans 7.4 IDE. I wrote a Singleton Design Pattern helper class and get an instance of the class using the getInstance() static method. I should get an auto-completion hint beside my instance variable, but surprisingly it wasn’t showing any hints which should show a list of all my public methods.

What’s wrong!!!

I check my class line by line. Nah, everything is OK. But it should work, but wasn’t working. I checked again, again, and again. When I started to pull my hair off, I stopped at a page of StackOverflow. I found a solution to my problem.

It is actually a feature of NetBeans IDE.

The solution to this problem is to write a comment block over the line that creates an instance of their Singleton class. The proper method of instantiation is:

/* @var $instance NewsSiteHelper */
$instance = NewsSiteHelper::getInstance();
// my method being called
$aa = "Date: ". $instance->getNewsDate();
echo $aa;

If I delete the comment line, I do not get the auto-complete hint.

Singleton class auto complete hint in NetBeans 7.4

This is my Singleton class:

<?php
defined('_JEXEC') or die;

class NewsSiteHelper
{
    protected static $context = "news.currentdate";
    
    public static $instance = null;
    
    public function __construct()
    {
        $app = JFactory::getApplication();
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        
        $query->select('a.created');
        $query->from($db->quoteName('#__adtv_news').' AS a');
        $query->order($db->quoteName('a.created'), 'DESC');
        
        $db->setQuery($query, 0, 1);
        
        $recentdate = $db->loadResult();
        
        $recentdate = empty($recentdate) ? JFactory::getDate() : $recentdate;
        $app->setUserState(self::$context, $recentdate);
    }

    /*
     * Singleton
     */
    public static function getInstance()
    {
        if (empty(self::$instance))
        {
            $instance = new NewsSiteHelper();
            self::$instance = $instance;
        }
 
        return self::$instance;
    }
    
    //
    public function __clone()
    {
        trigger_error("This class cannot be clonned.", E_USER_ERROR);
    }

    //
    public function getNewsDate()
    {
        $app = JFactory::getApplication();
        return $app->getUserState(self::$context);
    }
    
    //
    public function setNewsDate($date)
    {
        $app = JFactory::getApplication();
        $app->setUserState(self::$context, $date);
    }
}

I hope that it will save some of your valuable time.

Also read:  Store user state variables in Joomla 2.5/3.0
Category Joomla 3.0
Tag:

Leave Your Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.