Jump to content

"Use of undefined constant MYSQL_SERVER_1 - assumed 'MYSQL_SERVER_1'"


cgm225

Recommended Posts

I have a front controller that I use to serve up pages (all class code included at bottom of post), and within that class I am trying to access some constants (that specify my database configurations) found outside the class in a configuration file.  However, I continue to get "Use of undefined constant MYSQL_SERVER_1 - assumed 'MYSQL_SERVER_1'" type errors.  Is this a scope issue?

 

Any ideas?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Index.php5

    include_once PAGE_DIR . "/GeneralConfigurations.php5";
   
    //Requiring the front controller class
    require_once PAGE_DIR . "/FrontController.php5";

    FrontController::createInstance()->dispatch();

 

 

GeneralConfigurations.php5

    define(MYSQL_SERVER_1, "mysql.internal");
    define(MYSQL_SERVER_1_USERNAME, "user");
    define(MYSQL_SERVER_1_PASSWORD, "pass");

 

 

FrontController.php5

require_once PAGE_DIR . "/ActionController.php5";

class FrontController {

    //Checking if the PAGE_DIR variable is defined
    public static function createInstance() {
        if (!defined("PAGE_DIR")) {
            exit("Critical error: Cannot proceed without PAGE_DIR.");
        }
        
        
        $instance = new self();
        return $instance;
    }

    public function dispatch() {

        //The next two lines are ternary operators, a sort of short-hand for if-else
        $module= !empty($_GET["module"]) ? $_GET["module"] : "home";
        $action = !empty($_GET["action"]) ? $_GET["action"] : "index";

        //example GuestbookActions
        $class = ucfirst($module) . "Actions";

        //e.g. guestbook/GuestbookActions.php5
        $file = PAGE_DIR . "/" . $module . "/" . $class . ".php5";
        if (!is_file($file)) {
            exit("Module not found by FrontController Class: Dispatch Function");
        }

        //Requiring the Actions file
        require_once $file;

        //Creating a new object
        $controller = new $class();
        //Using the setName variable from the ActionController class in ActionController.php5
        $controller->setName($module);
        //Checks if the method exists, then runs the displayView action
        $controller->dispatchAction($action);
    }
}   

 

ActionController.php5

abstract class ActionController {

    protected $name;
    protected $viewData = array();

    public function setName($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    //This function places a value in the $viewData array at the postion indicated by the key
    public function setVar($key, $value) {
        $this->viewData[$key] = $value;
    }

    //This returns a value from the array located at the key value
    public function getVar($key) {
        if (array_key_exists($key, $this->viewData)) {
            return $this->viewData[$key];
        }
    }

    //method_exists checks if the class method exists in the given object, then runs the displayView function if exists
    public function dispatchAction($action) {
        $actionMethod = "do" . ucfirst($action);
        if (!method_exists($this, $actionMethod)) {
            exit("Page not found by ActionController dispatchAction()");
        }

        $this->$actionMethod();
        $this->displayView($action);
    }

    public function displayView($action) {
        if (!is_file(PAGE_DIR . "/" . $this->getName() . "/" . $action . "View.php5")) {
            exit("Page not found by ActionController displayView() ");
        }

        //Create variables for the template
        foreach ($this->viewData as $key => $value) {
            $$key = $value;
        }

        include PAGE_DIR . "/" . $this->getName() . "/" . $action . "View.php5";
    }

    public function __set($key, $value)  {
        $this->setVar($key, $value);
    }

    public function __get($key) {
        return $this->getVar($key);
    }
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.