使用php编写php文件后获取错误意外公开

My methods are inside class but I am still getting error of

error: syntax error, unexpected 'public' (T_PUBLIC), expecting end of file in E:\xampp\htdocs\crud\application\controllers\student.php on line 1.

May be that's because I am writing php file from php

PHP file writing code:

$file_data_write = "
".'public function '.$table_name.'(){' . "
" . '$this->load->view("'.$view_location.'");' . "
" . '}'. "
" . '}';
$file_location = APPPATH."controllers/". $view_and_controller_location;
$file_name = "/" . $view_and_controller_location . ".php";
$myfile = fopen($file_location . $file_name, "r+") or die("Unable to open file!");
fseek($myfile, -2, SEEK_END);
fwrite($myfile, $file_data_write);
fclose($myfile);

Class I am writing to

class Student extends CI_Controller {
  public function six(){
    $this->load->view("student/six");
  } 
}

What you probably doing is adding your function after class closing } or php closing syntax

class Test 
{

}
?>

public function ... {
}

there can't be "public" function outside class, what error is exactly telling you

or you are overwritting file not adding at end so public function will be in 1 line