Class not found in Joomla 4 when using JDate inside a function

by webchun   Last Updated May 07, 2019 00:10 AM

In my last question I tried to modify mod_latest_articles module, and added myCustomFunction to ArticlesLatestHelper.php like this :

 * @package     Joomla.Site
 * @subpackage  mod_articles_latest
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt

namespace Joomla\Module\ArticlesLatest\Site\Helper;

defined('_JEXEC') or die;

use Joomla\CMS\Access\Access;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\Component\Content\Site\Model\ArticlesModel;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;

\JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');

 * Helper for mod_articles_latest
 * @since  1.6
abstract class ArticlesLatestHelper
     * Retrieve a list of article
     * @param   Registry       $params  The module parameters.
     * @param   ArticlesModel  $model   The model.
     * @return  mixed
     * @since   1.6
    public static function getList(Registry $params, ArticlesModel $model)
    { // this is the built-in function ... }

    public static function myCustomFunction($created_date)
        $date = new JDate($created_date);
        echo $date->format('l, d F Y H:i', false, false);

I'm able to call the function from the default.php by using @Zollie's solution, but now I got this error message when using JDate inside my function

Error: Class 'Joomla\Module\ArticlesLatest\Site\Helper\JDate' not found: Class 'Joomla\Module\ArticlesLatest\Site\Helper\JDate' not found

Can anybody tell me, what am I doing wrong in here now? Thanks!

Answers 1

I’m answering the question because it is obviously not a Joomla 4 core bug but a mistake in the developer’s coding and the problem would be the same in Joomla 3 too.

If you try to use a Joomla core class in your code you have to include that class on the top of the file otherwise Joomla does not know what it needs to use, so:

use Joomla\CMS\Date\Date;

Then you can use in your code:

$date = new Date($created_date);

These classes are not automatically available everywhere across Joomla unless you point to them. So include the classes you use on the top of your files.

May 07, 2019 04:25 AM

Related Questions

Updated February 01, 2017 14:04 PM

Updated June 14, 2016 08:04 AM

Updated November 15, 2016 08:04 AM