Trying to create a function to show a news and echo it

But facting issue when

a. Using public - its showing 0 - syntax error, unexpected token "public", expecting end of file. Shouldnt public be used in it. If removing public then its working fine

b. I believe there is better way to rewrite code especially echo ShowNewsUpdates();

If can someone guide.

    // No direct access
    defined('_JEXEC') or die; 
    use \Joomla\CMS\Factory;
$menutype   = $params->get( 'menutype');
$roll   = $params->get( 'roll');
$type = 0;
    public function ShowNewsUpdates(){
        $db = Factory::getDBO();
        $query = "SELECT * FROM `#__easyblog_post` WHERE published=1 order by id desc limit 0,1 ";
        $rows = $db->loadObject();  
            $list.='<p><a href="index.php?option=com_easyblog&view=entry&id='.$rows->id.'&Itemid=1"><span>'.$rows->title.'</span></a></p>       
                <p>'.date("l, F d, Y",strtotime($rows->created)).'</p>      
                    <p><i>Know More</i></p>';
                $list.='<p>No News Added!!</p>';
            echo $list;
    echo ShowNewsUpdates(); 

1 Answer 1


You are writing procedural code but the 'public' statement is part of the object orientated approach to coding so it needs to be inside a Class.

For example you would normally use it like this

Class Someclass {

    public function yourfunction {

That is why your code works without the public statement, it is only recognised inside of a Class statement.

If your code is to be used inside an extension for Joomla then yes there are better ways to write your code but if it is for the purpose of accessing data from outside of Joomla in an ad-hoc or simple way then whatever works for you is fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.