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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:16:17+00:00 2026-06-15T19:16:17+00:00

I’ve got this autoloader method which is used to include class files as needed.

  • 0

I’ve got this autoloader method which is used to include class files as needed.

public static function autoloader($className) {

    $fileExists = false;
    foreach(array(LIBS_PATH, CONTROLLERS_PATH, MODELS_PATH) as $path) {

        // Check if the class file exists and is readable
        if(is_readable($classPath = $path . '/' . $className . '.php')) {
            // Include the file
            require($classPath);
            $fileExists = true;
            break;
        }
    }

    if($fileExists === false) {
        exit('The application cannot continue as it is not able to locate a required class.');
    }
}

That works fine but I was thinking is this way better:

public static function autoloader($className) {

    foreach(array(LIBS_PATH, CONTROLLERS_PATH, MODELS_PATH) as $path) {

        // Check if the class file exists and is readable
        if(is_readable($classPath = $path . '/' . $className . '.php')) {
            // Include the file
            require($classPath);
            return;
        }
    }
    exit('The application cannot continue as it is not able to locate a required class.');
}

As you can see I’m using the return statement in the middle of the loop to terminate the rest of the function because the class file has been included and the method has done its job.

Which way is the best way to break out of the loop if a match has been found? I’m just unsure about using the return statement because I always associated that with returning some value from a method.

  • 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-15T19:16:19+00:00Added an answer on June 15, 2026 at 7:16 pm

    Return can be used to simply break out of a method/function, and this use is perfectly legal. The real question to ask is what impact do you feel it has on readability?

    There are different schools of thought on early returns.

    One school maintains a single entry/exit point, stating that it makes the code easier to read since one can be assured that logic at the bottom of the code will be reached.

    Another school states that the first school is outdated, and this is not as pressing a concern, especially if one maintains shorter method/function lengths.

    A medium exists between the two where a trade-off between an early return and convoluted logic is present.

    It comes down to your judgment.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.