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 in 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 getInstance() static method. I should get 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 to write a comment block over the line that creates a instance of their Singleton class. The proper method of instantiation is:

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

 

If I delete the comment line, I do not get 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);
    }
}

Hope that helps you save your time.

Other articles of this category: