CodeIgniter Forums
Syntax error, unexpected 'public' (T_PUBLIC) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Choosing CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=8)
+--- Thread: Syntax error, unexpected 'public' (T_PUBLIC) (/showthread.php?tid=68002)



Syntax error, unexpected 'public' (T_PUBLIC) - luroti - 05-08-2017

Hy

I hope that I'm opening this topic in the correct section.

I started to study CI_tutorial and now i stopped from many hours by a problem about a news_model. 
When run the tutorial's example get the error: 


A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\httpg\application\models\news_model.php:10)
Filename: core/Common.php
Line Number: 570
Backtrace:


A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected 'public' (T_PUBLIC)
Filename: models/news_model.php
Line Number: 10
Backtrace:


This is the code of news_model.php
Tahnk you for help!
Luigi Confused


<?php

class News_model extends CI_Model 

{

        public function __construct()

        {

                $this->load->database();

        }

}



public function get_news($slug = FALSE)

{

        if ($slug === FALSE)                                            

        {

                $query = $this->db->get('news');                        

                return $query->result_array();                          

        }



        $query = $this->db->get_where('news', array('slug' => $slug));

        return $query->row_array();
}


RE: Syntax error, unexpected 'public' (T_PUBLIC) - xenomorph1030 - 05-12-2017

The error is exactly what it says, unexpected 'public' on line 10. Check out line 10. Check out your class and how it only contains a __construct() function.

PHP does a decent job most of the time telling you where an error is. Always check the simple things and don't be so quick to give up.


RE: Syntax error, unexpected 'public' (T_PUBLIC) - antony - 05-12-2017

The issue is with the get_news function, which is not in a class.
As it is not in a class, you can't declare it as public.
Either remove public, or put the function in the class, and you'll be fine.