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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:23:48+00:00 2026-05-17T00:23:48+00:00

In this question about including all the classes in a directory for an interpreter

  • 0

In this question about including all the classes in a directory for an interpreter that needs all of them, it was suggested that a better way to handle the problem would be to conditionally include only the Command file that is needed for this iteration of the interpreter. For example, instead of this:

require_once('CommandA.php');
require_once('CommandB.php');
require_once('CommandC.php');

class Interpreter {

    public function interpret($action) {
        switch($action) {
            case 'A':
                $c = new A();
                $c->execute();
                break;
        }
    }
}

Do something more like this:

class Interpreter {

    public function interpret($action) {
        switch($action) {
            case 'A':
                require_once('CommandA.php');
                $c = new A();
                $c->execute();
                break;
        }
    }
}

What are the advantages and disadvantages of going with the second choice — dynamically and conditionally loading only the needed command — as opposed to the first one — including all of the files at the head of the file, C-style?

In the answer given to my previous question, speed, footprint and ease of writing were listed as advantages of conditionally loading. In other questions where this is addressed, I’ve seen it suggested that c-style loading is more readable and maintainable. Can anyone elaborate on these? Any other advantages or disadvantages?

  • 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-17T00:23:48+00:00Added an answer on May 17, 2026 at 12:23 am

    The main advantage of the conditional loading is that it won’t include a whole tree of php files every time you load that class. Event when you use a code cache (and you should), the files are checked for updates every time the vm encounters an /(include|require)(once)?/, so including unnecessary php files will slow down your code.

    The disadvantages of this particular method is, that it is harder to maintain, and easier to make mistakes. Writing one thing twice is a code smell. Wirting My_Class means, that you need this class, and writing include “My_Class.php” also means this. This is not right.

    I suggest using autoloading: http://php.net/manual/en/language.oop5.autoload.php

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

Sidebar

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.