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 7711159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:18:20+00:00 2026-06-01T01:18:20+00:00

I’m making a small PHP MVC framework for fun. I’m using a front controller

  • 0

I’m making a small PHP MVC framework for fun. I’m using a front controller index.php to route all traffic around and call new controllers as requested. However, I need a way to create an instance of a user generated controller base solely on the name of the controller (basically, the filename ie, controllers/posts.php).

Is there any way to do this?

  • 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-06-01T01:18:21+00:00Added an answer on June 1, 2026 at 1:18 am

    There are a couple ways you can do this. One way is to establish a convention, such that the name of the class is based on the name of the file. Another way to do it would be to parse the file for the name of the class. Did you want to see some small code examples?

    EDIT: Simple Example

    Imagine we’re in some core MVC controller and we want to load a controller provided by a third party user. There are 2 things you need to do:

    1. Load the file with the class definition
    2. Instantiate the class

    Suppose you have a convention such as Zend where class names map to the filesystem so a controller might be

    MyProject/Controller/Login.php

    for the path to the login controller, relative to the root of the project. Following the Zend convention, the name of the class would be MyProject_Controller_Login. If you want to instantiate this class all you have to do is:

    // load class
    require_once 'MyProject/Controller/Login.php';
    
    // instantiate class
    $oUserController = new MyProject_Controller_Login();
    

    If you need to instantiate the class based on runtime data, you can do so with a variable that holds the name of the class, so

    $sUserController = 'MyProject_Controller_Login';
    $oUserController = new $sUserController();
    

    Hopefully that’s enough to get you rolling; let me know if you need more.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.