shevado shevado - 5 months ago 21
PHP Question

Method of CI 3 doesn't work on php 5.6 version but works on php 7

I am using Codeigniter 3.0.6. And i am developing on my local server and my php version is already php7. It works well. But then i uploaded it on server that use php 5.6. Then i got this error.


Parse error: syntax error, unexpected 'list' (T_LIST), expecting identifier (T_STRING) in /var/www/simimi/application/controllers/Student.php on line 53
A PHP Error was encountered

Severity: Parsing Error

Message: syntax error, unexpected 'list' (T_LIST), expecting identifier (T_STRING)

Filename: controllers/Student.php

Line Number: 53

Backtrace:



Here's my controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Student extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->model('m_student','sdb');
$this->load->model('m_student_profile','sdb_pro');
$this->load->model('m_student_academic','sdb_aca');
$this->load->model('m_student_immigration','sdb_imm');
$this->load->model('m_student_emergency','sdb_eme');
$this->cname = 'student';
$this->menu = 'Student';
$this->fitur = '';
$this->active_user=get_nama_user();
$this->active_username=get_username();
$this->active_privilege=get_hak_akses();
if(!cek_auth())
{
flash_err('Authorization needed.');
redirect(base_url('auth'));
}
if(!cek_fitur('student_list'))
{
flash_err("You don't have privilege to use `{$this->menu}` feature.");
redirect(base_url('dashboard'));
}
}

public function index()
{
$this->list();
}

public function action($func='', $id=0)
{
if(!empty(trim($func)))
{
if(!empty($id))
$this->$func($id);
else if(empty($id))
$this->$func();
}
else
{
flash_err("You don't have permission.");
redirect(base_url($this->cname));
}
}

public function list()
{
$data['title']='Student';
$data['subtitle']='List';
$data['active']='student_list';
$this->fitur = 'List';
$data['content']='student_list';
$data['students']=$this->sdb->get_list();
$this->load->view('template/template',$data);
}
}


Aaaaand
It just occured to me that list isnt allowed to be a method name. I wonder why it runs on php7 though.

Answer

This is due to the fact that list is a reserved keyword for PHP. Prior to PHP 7, you could not use these as method names.

http://www.php.net/manual/en/reserved.keywords.php

As of PHP 7.0.0 these keywords are allowed as as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name.