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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:05:55+00:00 2026-06-18T23:05:55+00:00

For my project I’ve wrote a basic controller class: <?php abstract class Controller {

  • 0

For my project I’ve wrote a basic controller class:

<?php
   abstract class Controller
   {
      static $__instance = null;

      protected function __construct()
      {
      }

      final private function __clone()
      {
      }

      public static function getInstance()
      {
         $class = get_called_class();

         return self::$__instance ? self::$__instance : (self::$__instance = new $class());
      }
   }
?>

Now every controller does inheritance from this controller.

Like this:

<?php

   include_once 'model/page.php';
   include_once 'view/page.php';

   class PageController extends Controller
   {
      private $m_model = null;

      private $m_view = null;

      private $m_id;

      protected function __construct()
      {
         parent::__construct();

         $this->m_id = uniqid();

         $this->m_model = new PageModel();
         $this->m_view = new PageView();
      }

      public function preparePage()
      {
         echo 'Hello';
      }
   }
?>

And in my index.php I got this:
$user = UserController::getInstance();
$page = PageController::getInstance();
var_dump($page);

The problem is that the var_dump($page) shows that the variable $page is type of UserController, but why? It should be of the type PageController. Any ideas?

  • 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-18T23:05:56+00:00Added an answer on June 18, 2026 at 11:05 pm

    Because there’s only one self::$__instance which is shared among all classes that inherit from Controller. You can use an array to keep track of multiple instances:

    return self::$__instance[$class] ? self::$__instance[$class] : (self::$__instance[$class] = new $class());
    

    But really, singleton patterns are frowned upon. You should instantiate new PageController once at a defined location and inject it down where it is needed. Then there’s no need for this static singleton constructor.

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

Sidebar

Related Questions

My project consists of an app that links to two static libraries. Each of
Project CompetitionServer.exe raised exception class ESQLiteException with message 'Error executing SQL. Error [1]: SQL
project is written on php. There is timestamp field in mysql it updates automatically.
Project Structure I have a silverlight project SLProj, that references a silverlight class library
Our project is using many static libraries to build the application. How can we
Project ID: com.abc.def.MyProject:pom:null Reason: Cannot find parent: com.xyz.list.MyWonderfulFamilly for project com.abc.def.MyProject:pom:null for com.abc.def.MyProject:pom:null [INFO]
Project Background: I am writing a map tile overlay class for java that can
Project I've developed a remoting class which is used to replace a subset of
Project: http://design.vitalbmx.com/fave/news.html When I click Add to favorites button (under main pic), in Chrome
Project layout: /project_a /shared /project_b /shared /shared project_a and project_b both need to contain

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.