Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda