PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am trying to write up a simple MVC framework, mainly to get some practice and to learn more about PHP. I finished writing the main framework controller, which will call the other controllers which will in turn process and subsequently display the requested pages. I created a trait to be used by all controllers that will display a type of "Page Not Found" message. When I try calling the trait from within any class, the interpreter gives me an error message pointing to the class calling the trait. I used CPanel to switch between PHP 5.4 and 5.5; however, I continue to get the same error. I even looked at the tutorial on the PHP Manual website, but unless I am overlooking something I have not been able to find a way to make this work. Any help will greatly be appreciated.
// Trait file: PageNotFound.php
<?php
trait PageNotFound {
public function showPageNotFound(){
// Code here...
}
}
?>
// Class file: MainController.php
<?php
require_once "PageNotFound.php"
class MainController {
use PageNotFound;
public function __construct(){}
// Code here...
?>
$controller = new MainController();
$controller -> showPageNotFound(); // The interpreter cites the use of "USE" as an error within the MainController class.
Last edited by xtianos on Thu Mar 13, 2014 12:28 am, edited 1 time in total.
/*
* This is the main controller class that is called
* every time the page is accessed. As previously
* mentioned, this controller calls the others
* based on the user's request. The file's name is
* the same as the class name.
*/
<?php
class Controller {
private $_pageRequest;
private $_controllersList;
private $_controllerRequest;
public function __construct(){}
protected function _buildControllersList(){
// Code here...
}
protected function _processPageRequest(){
// Code here...
}
public function getControllerRequest(){
// Code here...
}
public function getPageRequest(){
// Code here...
}
public function getControllersList(){
// Code here...
}
final public function execute($defaultController = "Main", $defaultPage = "showPageNotFound"){
// Code here...
require_once CONTROLLER_PATH . "/" . $defaultController . ".php";
call_user_func(array(new $defaultController(), $defaultPage));
}
}
?>
/*
* This is the controller called by the controller
* defined above. Like before, the name of this
* new controller is the same as the class.
*/
<?php
require_once "/home/username/framework/models/traits/PageNotFound.php";
class Main {
use PageNotFound;
public function __construct(){}
public function LoginPage(){
// Code here...
}
public function LogoutPage(){
// Code here...
}
}
?>
/*
* The trait referenced above. Like the two classes above,
* the name of the trait is the same as the name of the file.
*/
<?php
trait PageNotFound {
public function showPageNotFound(){
echo "Trait::PageNotFound.";
}
}
?>
Below is the index.php file. As previously mentioned, I am trying to figure out what is missing because by studying the code I cannot come up with the answer. I have re-written the whole code at least three times and still cannot figure it out. The message I keep getting when the index file runs is that an unexpected USE is being used, followed by the path of the file and the line where the USE is located/called. Basically, when the index file runs, the error message always points out to the class using the trait, in this case Main. I am wondering if there is something that needs to be changed with the php.ini file or perhaps at the ISP, other than that I do not know what else to do to figure this one out. Thanks for taking the time.
// The index file.
<?php
// Framework constants
define("FRAMEWORK_PATH", "/home/username/framework");
define("CONTROLLER_PATH", FRAMEWORK_PATH . "/controller");
define("MODELS_PATH", FRAMEWORK_PATH . "/models");
define("VIEWS_PATH", FRAMEWORK_PATH . "/views");
require_once CONTROLLER_PATH . "/Controller.php";
$controller = new Controller();
$controller -> execute();
?>
// The Controller file.
<?php
class Controller {
private $_pageRequest;
private $_controllersList;
private $_controllerRequest;
public function __construct(){
$this -> _processPageRequest();
$this -> _buildControllersList();
}
protected function _buildControllersList(){
$controllers = scandir(CONTROLLER_PATH);
$this -> _controllersList = array_slice($controllers, 2);
}
protected function _processPageRequest(){
$uriArray = explode("/", $_SERVER["REQUEST_URI"]);
$cleanRequest = array();
foreach($uriArray as $value){
if(!empty($value)){
$cleanRequest[] = $value;
}
}
$this -> _controllerRequest = $cleanRequest[0];
$this -> _pageRequest = $cleanRequest[1];
}
public function getControllerRequest(){
return $this -> _controllerRequest;
}
public function getPageRequest(){
return $this -> _pageRequest;
}
public function getControllersList(){
return $this -> _controllersList;
}
public function execute($defaultController = "Main", $defaultPage = "PageNotFound"){
if(empty($this -> _controllerRequest)){
require_once CONTROLLER_PATH . "/" . $defaultController . ".php";
call_user_func(array(
new $defaultController(),
"LoginPage"));
}else if(in_array(
$this -> _controllerRequest . ".php",
$this -> _controllersList)){
require_once CONTROLLER_PATH . "/" . $this -> _controllerRequest . ".php";
$controller = new $this -> _controllerRequest();
if(method_exists(
$controller,
$this -> _pageRequest)){
call_user_func(array(
$controller,
$this -> _pageRequest));
}else{
call_user_func(array(
new $defaultController(),
$defaultPage));
}
}else{
require_once CONTROLLER_PATH . "/" . $defaultController . ".php";
call_user_func(array(
new $defaultController(),
$defaultPage));
}
}
}
?>
// The Main controller file.
<?php
require_once "/home/username/framework/models/traits/PageNotFound.php";
class Main extends Controller {
use PageNotFound;
public function __construct(){
parent::__construct(){}
/*
public function PageNotFound(){
echo "Main::Page Not Found.";
}
*/
public function LoginPage(){
echo "Main::Login page.";
}
public function LogoutPage(){
echo "Main::Logout Process complete.<br />";
$this -> LoginPage();
}
public function execute(){
if(method_exists($this, $this -> getPageRequest())){
echo $this -> getControllerRequest() . "::" . $this -> getPageRequest();
}
}
}
?>
// The PageNotFound trait file.
<?php
trait PageNotFound {
public function PageNotFound(){
echo "Trait::PageNotFound.";
}
}
?>
The error message on the browser:
Parse error: syntax error, unexpected T_USE, expecting T_FUNCTION in /home/username/framework/controller/Main.php on line 5
The phpinfo() showed the problem, although CPanel was allowing me to set the PHP version to 5.5, somehow the server continued to handle all .php files with version 5.3. I do not understand how this happened because I was using 5.4 until a few months ago when 5.5 became a stable version and available on my ISP. Anyway, I placed a trouble ticket with my ISP so they can look into this because other people may have been or are experiencing a similar problem.
Thank you very much for taking the time to answer my post. Cheers!