Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
Is a PHP class always globally available, even if defined inside function?

I need to find out, if a php class is always guaranteed to be globally available after it was declared the first time.

Example:

// in File A:
function check_if_mail() {
    if ( ! empty( $_GET['mail'] ) ) {
        class MyMail {}

        // now I do some require_once() calls to extend the class.
    }
}

// This function is not always executed...
if ( /* some_conditions */ ) {
    check_if_mail();
}


// In File B:
if ( class_exists( 'MyMail' ) ) { // <-- will this work?
    require_once 'template_mail.php';
} else {
    require_once 'template_page.php';
}

"File A" is written by me, while "File B" is inside a third party library. Also File A is quite big and is a class itself, so I would like to keep the function/condition structure if possible.

➜ I need to make sure, that the condition in File B will correctly trigger in PHP 5.6 and 7.0.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Is there a specific reason you are putting it in a function since you are immediately calling that function anyways? Why not just use it:

// in File A:
if ( ! empty( $_GET['mail'] ) ) {
    class MyMail {}

    // now I do some require_once() calls to extend the class.
}

// In File B:
if ( class_exists( 'MyMail' ) ) { // <-- will this work?
    require_once 'template_mail.php';
} else {
    require_once 'template_page.php';
}

PHP code does not need to be encapsulated in a function.

Edit: Based on your current edit, it would probably be better to move the class into another file, check your condition, and run an include:

// class-my-mail.php
class MyMail {}

// in File A:
function check_if_mail() {
    if ( ! empty( $_GET['mail'] ) ) {
        require_once 'class-my-mail.php';
    }
}

// This function is not always executed...
if ( /* some_conditions */ ) {
     check_if_mail()
}

Although this seems a little...complicated. But the short answer is 'yes'. As long as the class is included before checking for it, it is globally available in 5.6 and 7. Although 5.6 isn't supported any more so I don't know how much I would worry about that.

Also, you're doubling up on the conditions in file A. Is there a reason you need conditions outside check_if_email? Or outside the class even?

It seems like all of this would be better just in the class. Include the class, put the validation in the class functions and pass the parameters to it. That would be much more organized and easy to understand and extend later. Because right now you're risking having changing conditions all over the place, especially with 3rd-party code involved.

over 4 years ago · Santiago Trujillo Report

0

Yes.

Class definitions (and function definitions) are global in PHP. They are not variables; defining one inside a function does not limit the definition to that scope.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!