CodeIgniter Forums

Full Version: unexpected 'public' (T_PUBLIC)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.


PHP Code:

class Pages extends CI_Controller {


public function view($page 'home')
       if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
               // Whoops, we don't have a page for that!

$data['title'] = ucfirst($page); // Capitalize the first letter




How to fix this error line # 13?

Parse error: syntax error, unexpected 'public' (T_PUBLIC) in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\blog\application\controllers\Pages.php on line 13
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected 'public' (T_PUBLIC)
Filename: controllers/Pages.php
Line Number: 13

Line 12 - 13:   if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))


I do not know how to fix the error.  Can anyone help?
Might sound daft, by try removing the closing php tag.

(Might not help too - but I never include them.)
Remove the "public" keyword before the controller function. Surely that's what it means when it says: "unexpected 'public'".
Using public or private is a good idea though, and should not in itself cause an issue. Usually this happens when I forget the curly brace but he has it.

He also has removed the basepath check, which was probably him trying to fix another problem, I suspect he has got the whole file structure and CI implementation wrong in the first place.

PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed'); 

I think he needs to learn PHP before trying to learn CodeIgniter.
try removing that closing: ?>

its probably the problem. if its not, then post again and someone will try to help.
what is not a problem - is you learning how to code. i salute you doing the right thing and working through the tutorial. i encourage your efforts. it is difficult and confusing at first, but it does get easier. and you will find that sometimes the issues that take the longest to solve - are just a few characters in length.
The provided code probably has not been pasted exactly. I think there is something between the class declaration and the first method that we can not see.
That would make sense, then an un-closed if statement, say, would cause the unexpected public declaration.
Perhaps the missing constructor?