Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6100039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:20:28+00:00 2026-05-23T13:20:28+00:00

All, I am reading the following article on a lightweight PHP dynamic front controller:

  • 0

All,

I am reading the following article on a lightweight PHP dynamic front controller: http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5

Here is the code:

index.php

<?php

define("PAGE_DIR", dirname(__FILE__) . "/pages");
require_once "FrontController.php";
FrontController::createInstance()->dispatch();

FrontController.php

<?php

class FrontController {
  public static function createInstance() {
    if (!defined("PAGE_DIR")) {
      exit("Critical error: Cannot proceed without PAGE_DIR.");
    }
    $instance = new self();
    return $instance;
  }
  public function dispatch() {
    $page = !empty($_GET["page"]) ? $_GET["page"] : "home";
    $action = !empty($_GET["action"]) ? $_GET["action"] : "index";
    //e.g. HomeActions
    $class = ucfirst($page) . "Actions";
    //e.g. pages/home/HomeActions.php
    $file = PAGE_DIR . "/" . $page . "/" . $class . ".php";
    if (!is_file($file)) {
      exit("Page not found");
    }
    require_once $file;
    $actionMethod = "do" . ucfirst($action);  

    $controller = new $class(); // I DON'T UNDERSTAND WHAT THIS DOES...  

    if (!method_exists($controller, $actionMethod)) {
      exit("Page not found");
    }
    //e.g. $controller->doIndex();
    $controller->$actionMethod();
    exit(0);
  }
}

pages/guestbook/GuestbookActions.php

<?php

class GuestbookActions {
  public function doIndex() {
    echo "Index action called...";
  }
  public function doCreatePost() {
    echo "CreatePost action called...";
  }
}

In the front controller class, could someone explain to me what $controller = new $class(); does? I don’t understand it. It seems to be creating a class on the fly? In the example above, $class is a string with a value like "HomeActions". So $controller would be a new instance of a class named "HomeActions", but those are not defined anywhere. I’m confused.

Many thanks,

JDelage

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:20:28+00:00Added an answer on May 23, 2026 at 1:20 pm
    $controller = new $class();
    

    That does indeed create a new object of the type contained in $class, so it is equivalent to $controller = new HomeActions() in your example. From the manual:

    If a string containing the name of a class is used with new, a new instance of that class will be created

    The classes are not all present initially. However, the necessary one is loaded dynamically:

    $file = PAGE_DIR . "/" . $page . "/" . $class . ".php";
    if (!is_file($file)) {
      exit("Page not found");
    }
    require_once $file;
    

    require_once loads the file which presumably contains the class definition, so you can create the object as shown above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading all the examples here and elsewhere I wrote the following code to
I was reading the following article: http://msdn.microsoft.com/en-us/magazine/cc817398.aspx Solving 11 Likely Problems In Your Multithreaded
I am reading the following article, http://articles.sitepoint.com/article/hierarchical-data-database/2 aboutStoring Hierarchical Data in a Database. This
I was reading the following article http://blogs.msdn.com/b/wenlong/archive/2008/08/13/orcas-sp1-improvement-asynchronous-wcf-http-module-handler-for-iis7-for-better-server-scalability.aspx and I am a little confused. First
I am reading an article on extension of interface at following link. http://wiki.hsr.ch/APF/files/ExtensionInterface.pdf It
I am following the spring MVC tutorial and also reading this article on mvc
The following code reads messages from other processes through a pipe. All processes correctly
I am reading about Dynamic programming in Cormen etc book on algorithms. following is
I was reading this article => http://mgutz.com/2010/10/01/check_gmail_with_node_js.html . Below in the comments Shomrom says:
I'm quite new to Javascript and I was just reading following article. you can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.