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

  • Home
  • SEARCH
  • 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 3593730
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:36:59+00:00 2026-05-18T19:36:59+00:00

I’m studying this code: http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5 In it the author uses a static function to

  • 0

I’m studying this code:
http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5

In it the author uses a static function to instantiate a class. I’m basically a beginner and I had never seen this. Why would one use a static instantiator rather than the usual constructor?

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() {....} 
  • 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-18T19:37:00+00:00Added an answer on May 18, 2026 at 7:37 pm

    This is a workaround for PHP, since it is too dumb for stuff like this:

    (new SomeClass())->doSomething();
    

    Oneliners like that are impossible in native PHP. That is why some people wrap the instantiation of the class in a static method to make it possible:

    SomeClass::create()->doSomething();
    

    It helps to keep the scope clean, since you do not need extra variables. It would look like this, otherwise:

    $instance = new SomeClass();
    $instance->doSomething();
    unset($instance);
    

    EDIT: let me quote Gordon here (from the comments):

    Static calls are the same as putting a function into the global
    scope. Calling it will always have a dependency on the global scope.

    You should be aware of this fact, as it makes your code less flexible.

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

Sidebar

Related Questions

No related questions found

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.