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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:18:20+00:00 2026-05-27T11:18:20+00:00

I am making a simple PHP application with an MVC design. All of the

  • 0

I am making a simple PHP application with an MVC design. All of the requests are being sent to index.php and then routed to the appropriate controller. Because all of the requests go through index.php, I want to create some objects there and be able to use them globally, regardless of which controller handles the request. Namely, these objects are of type User, ErrorHandler, and Database.

Having a global User class, for example, allows any controller to check if the user is logged in by checking $user->loggedIn (or something like that) where $user is instantiated in index.php.

Here is what I am describing in code:

// index.php:

<?php
    include('lib/User.php');

    $user = new User();

    // other stuff that index.php needs to do, perhaps route to FooController
?>

// FooController.php:

<?php
    class FooController {
        function __construct() {
            global $user;        

            if ($user->loggedIn) {
                // do whatever
            }
        }
    }
?>

This is just random code I just typed up so please excuse any stupid errors — but I hope you get the idea. This code will work, but the global $user; line must be included in every function inside FooController.php.

This will get annoying when every function must have three global statements, one for User, ErrorHandler, and Database. Is there a better way to go about this, while preserving my MVC design?

  • 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-27T11:18:21+00:00Added an answer on May 27, 2026 at 11:18 am

    I ended up doing this:

    class BaseController
    {
        public $user, $db, $errors;
    
        function __construct()
        {
             global $user, $db, $errors;
    
             $this->user = $user;
             $this->db = $db;
             $this->errors = $errors;
        }
    }
    

    Every controller extends the BaseController (which has some abstract functions as well, this is just an example of a BaseController which solves this specific problem). This way I only had to use global once, and then I could use the user, db, and error handler instances across every controller by simply calling parent::__construct() in each controller’s constructor and then referring to them by $this->user, etc.

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

Sidebar

Related Questions

I'm making a simple global get configuration. try to make a index.php function config(
I'm making a simple registration system in php however, even if i enter all
I'm making simple http post request using libcurl to index.php file on my web
I'm making a simple scheduler with C# in .Net. All it does is execute
I am making a simple PHP and JavaScript multiplayer game. Here is a quick
Making a simple three question PHP quiz. Each question is displayed on a page
I am making a simple Dynamic Website using PHP, where i allow the user
I'm making a simple facebook canvas app (page tab) with PHP, that posts something
I have a simple iPhone application I am making that will upload pictures to
I'm new to php and am making a simple login system. When someone wants

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.