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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:55:11+00:00 2026-06-13T21:55:11+00:00

I have a PHP class that needs some pre-defined globals before the file is

  • 0

I have a PHP class that needs some pre-defined globals before the file is included:

File: includes/Product.inc.php

if (class_exists('Product')) {
    return;
}

// This class requires some predefined globals
if ( !isset($gLogger) || !isset($db) || !isset($glob) ) {
    return;
}

class Product
{
   ...
}

The above is included in other PHP files that need to use Product using require_once. Anyone who wants to use Product must however ensure those globals are available, at least that’s the idea.

I recently debugged an issue in a function within the Product class which was caused because $gLogger was null. The code requiring the above Product.inc.php had not bothered to create the $gLogger. So The question is how was this class ever included if $gLogger was null?

I tried to debug the code (xdebug in NetBeans), put a breakpoint at the start of Product.inc.php to find out and every time it came to the if (class_exists(‘Product’)) clause it would simply step in and return thus never getting to the global checks. So how was it ever included the first time?

This is PHP 5.1+ running under MAMP (Apache/MySQL). I don’t have any auto loaders defined.

Thanks for the informative answers guys. My belief was that when you
include a file PHP starts executing it line by line from line one, so
it would not allow me to include the file if the globals were not
defined. I will move the checks into the constructor. Based on the
original question, I accept the answer from @deceze

  • 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-13T21:55:13+00:00Added an answer on June 13, 2026 at 9:55 pm

    The file is parsed before it is executed. Classes are “loaded” by parsing, but functions are executed after the parsing. By putting the function call in the same file as the class, the class is always parsed and “loaded” before that function executes, thereby it’s always true.

    If you are always including the file using require_once (which is good), there’s no point in that check anyway. A class definition shouldn’t conditionally depend on some global variables. Rethink what you’re doing here.

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

Sidebar

Related Questions

My problem: require_once '/includes/aws-sdk-1.5.2/sdk.class.php'; My environment: I have a pretty standard PHP site that
I have a PHP class that stores a complex multidimensional array, and rather than
I have a php class that generates a map image depending on my db
I have certain PHP class methods that access external variables. These variables are not
In PHP, is it possible to have a function within a class that's non-static,
I have three files: one called sql.php witch has a class db that I
I have a custom MVC PHP framework that has a router class, which calls
I have a project that needs to access a DLL with PHP. The server
I have a static function in a class that needs to be called from
I am working on some PHP classes, I have a session class used to

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.