Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda