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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:34:04+00:00 2026-05-31T05:34:04+00:00

I am building a custom MVC framework using PHP. My problem is when I

  • 0

I am building a custom MVC framework using PHP. My problem is when I want to access any model class through the controller class. One way I have seen this done is through a registry design pattern using magic methods such as get and set, though PHP get and set are considered bad practise by some. I have read about dependency injection done through a container, but I can not see this working effectily as the container would have to call the models or it would have to contain the models which would defeat the purpose of MVC and create a massive super class. Singleton is seen as bad practise. Is there any solutions or improvements of the methods I have mentioned. It may just be my understand and knowledge of PHP needs improving.

Currently I have this: router.php (loads up controllor through a GET varible

 <?php

class router {



function __construct() {

    if (file_exists("controller/".$_GET['url']."Controller.php"))  {


       function __autoload($controller) {

           $controlinclude = "controller/".$controller.".php";
            include $controlinclude;

      }
       $control = $_GET['url']."Controller";
        new $control();


    }
    else    {

        // throw exception
    }

}

}
?>

Hope that makes sence

  • 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-31T05:34:05+00:00Added an answer on May 31, 2026 at 5:34 am

    First of all … Do no put autoloading script in routing mechanism. You are mixing the responsibilities. You will be better off creating a separate class for this based on spl_autoload_register.

    Neeext .. do no put complicated operations on constructor. It is makes you code somewhat untestable. Maybe you should be something like:

    // you might want to replace $_GET with $_SERVER['QUERY_STRING'] later
    $router = new Router( $_GET['url'] );
    
    // where 'default' is the name of fallback controller
    $controller_class = $router->get_controller( 'default' );
    $method_name = $router->get_action( 'index' );
    
    $model_factory = new ModelFactory( new PDO( ... ) );
    $controller = new {$controller_class}( $model_factory );
    $controller->{$method_name}();
    

    Additionally, you should look into php namespaces. There is no point in ending class with ...Controller just to know where the class will be located.

    Ok … back to the Model.

    There is quite common misconception about models in web development community ( i blame RoR for this mess ). Model in MVC is not a class, but an application layer which contains multitude of instances. Most of the instances belong to one of two types of classes. With following responsibilities:

    • Domain Logic :

      Deals with all the computation, calculation and all the domain specific details. Objects in this group have no knowledge of where and how data is actually stored. They only manipulate the information.

    • Data Access

      Usually made of objects that fit DataMapper pattern (do not confuse with ORM of same name .. nothing in common). Responsible for storing data from Domain Objects and retrieving them. Might be in database.. might not. This is where your SQL queries would be.

    In semi-real world situation () it might looks something like this (related to code abowe):

    class SomeController
    {
       // ... snip ...
       protected $model_factory = null;
       // ... snip ...
    
       public function __construct( ModelFactory $factory )
       {
           $this->model_factory = $factory;
       }
       // ... snip ...
       
       public function action_foobar()
       {
          $user = $this->model_factory->build_object( 'User' );
          $mapper = $this->model_factory->build_mapper( 'User' );
    
          $user->set_id(42);
          $mapper->fetch($user);
    
          if ( $user->hasWarning()  )
          {
              $user->set_status( 'locked' );
          }
    
          $mapper->store( $user );
       }
    
       // ... snip ...
    }
    

    As you see, there is no indication how the data was stored. It does not even matter if user account was new, or already existing.

    Some materials you might find useful

    Videos

    • Advanced OO Patterns (slides)
    • Clean Code Talks: Don’t Look For Things!
    • Clean Code Talks: Unit Testing
    • Clean Code Talks: Global State and Singletons

    Books:

    • Real-World Solutions for Developing High-Quality PHP Frameworks and Applications
    • Patterns of enterprise application architecture
    • Clean Code: A Handbook of Agile Software Craftsmanship
    • SQL Antipatterns: Avoiding the Pitfalls of Database Programming
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just created a mid-sized web-application using Java, a custom MVC framework, javascript.
I've recently begun building version 2 of my year-old custom MVC framework. It's worked
I'm building a custom PHP Session handler using MySql, and I've been struggling with
I am building an MVC application and am designing a custom model binder for
I'm building a custom Ubuntu kernel and have modified one of the source files.
I am building a custom Content Management System for my client, using C#2008, ASP.Net
I'm building an ASP.NET MVC site where I want to decorate my ViewModels with
I'm building a multi-tenant app with ASP.NET MVC and have a problem with validating
I am currently building a custom Membership Provider for my Asp.net MVC website. I
We are currently building a mid-sized intranet application using MVC3. One requirement is the

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.