Return of a function in another Class

Asked

Viewed 49 times

0

I want to call the function data_hora_atual() in that session class:

class sessao {
    function fazSessao($conexao){

    }
}

class administracao {
    function data_hora_atual($par,$conexao){
        date_default_timezone_set('America/Sao_Paulo');

        return array();
    }
}
  • class sessao extends administration would not solve ?

  • 2

    It would be better to learn the basics before venturing into OO. For example, you run the risk of your date becoming corrupted if you take the turn of the day (or the hour or minute, whatever) right at the time of construction.

1 answer

0


Whereas you will use the data_hora_atual within the fazSessao, for example only:

Two alternatives:

Turn the function into statics:

function static data_hora_atual($par,$conexao){ }

And in Class Session:

function fazSessao($conexao){
    administracao::data_hora_atual($par, $conexao)
}

You can also extend your administration class:

class sessao extends administracao {
    function fazSessao($conexao){
        $this->data_hora_atual($par, $conexao);
    }
}
  • 1

    You don’t define a static method just to make it easier to call you, you define it when it should, in fact, be a static method. In the inheritance, even worse...

  • Please explain what will be best

  • First, to understand the problem, which is not clear in the question, and then to propose a solution. The best hypothesis of the moment would be, if it makes sense for this application, to create the class instance. But without the problem of course, there is no way to define how to do.

  • 1

    I’m sorry, as far as I’m concerned, it’s clear and objective. Obviously more details would help, but in short he wants to understand how to use a method of another class only. No bureaucracy, standards or good practice. If what he needs is to solve just that, both alternatives can help. If he wants to move forward, it’s different. But let’s consider that other people come here to read, without understanding much more and just need to solve.

  • 1

    I don’t mean to be rude, don’t get me wrong. But I believe it’s not legal for you to criticize and not propose a solution for those who are in doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.