Doubt about inheritance in classes

Asked

Viewed 211 times

2

How to do the if controller return TRUE or FALSE in accordance with the check() of the Validator?

Currently it returns boolean to the validate();, but I wanted it to continue until the end of the code, to then return the boolean.

<?php
# relatorio venda controller
class RelatorioVendaController {
    public function __construct() {
        $form['nome'] = 'Ricardo';
        $form['idade'] = 24;

        if(RelatorioVenda::validate($form)){
            print 'Nenhum erro.';
        }
    }
}

# relatorio venda model
class RelatorioVendaModel extends RelatorioModel {
    public static function validate($form){
        $validators = array(
            "nome" => "required",
            "idade" => "required"
        );
        parent::rules($form, $validators);
    }
}

# relatorio model
class RelatorioModel {
    public static function rules($form, $validators){
        // junta os arrays foreach e executa a validacao
        Validator::check($field, $map['field'], $map['validator']);
    }
}

class Validator {
    public static function check($field, $value, $validator){
        // execute validator(field, value)

        if(no erros)
            return true;
    }
    public function required($field, $value){
        // validator
    }
}
?>
  • check your check function, there is a syntax error in if(no erros) Edited: the function rules of the Report Model class needs a return also

  • I think the right thing would be if(no errors)

  • is just marking, this class is conceptual.

  • 3

    Can you clarify further what you want the code to do? I did not understand the part of going to the end of the code. Another thing, "object orientation" means not only having classes and inheritance but also encapsulation, which is lost with how many static functions (Static). This is practically like creating global functions. You should re-organize the code to get rid of static functions if possible.

  • Where does that come from $form in the Controller constructor? The.o

  • @Henriquebarcelos From anywhere, it can be a form, as here is just an example is implicit in the code.

  • @Jonashartmann When I call the Reportproposal::validate(), it performs this function and returns boleanus according to that scope. I would like it to follow "Parent::Rules($form, $validators)" and depending on the result there, it returns there in the controller. It gets a little confusing to explain, but you’ve managed to understand?

Show 2 more comments

2 answers

1


You can use a variável!

    $booleano = RelatorioVenda::validate($form);
    if($booleano){
        print 'Nenhum erro.';
    }
    return $booleano;

Don’t forget to use return functions to return some result...

... but remember that the indirect call to a builder via operator new always returns a new instance of a class, and never a boolean or any other type of result.

So you should move that part of your code to another class method.

  • 1

    In fact, __construct returns nothing. There should be no statement return in the body of the builder.

  • Is it wrong to say that __construct does not return anything: http://stackoverflow.com/a/11904285/370290 - although, evidently, this use is completely inappropriate.

  • Anyway, I improved on my answer based on your feedback. Thank you.

0

Remember that builders have no return.

The best way to do what you want is through exceptions:

# relatorio venda controller
class RelatorioVendaController {
    public function __construct($form) { //editei essa linha pra fazer sentido
        $form['nome'] = 'Ricardo';
        $form['idade'] = 24;

        if(RelatorioVenda::validate($form)){
            print 'Nenhum erro.';
        } else {
            throw new Exception('Houve algum erro de validação');
        }
    }
}

When it’s time to use, you do:

try {
    $controller = new RelatorioVendaController($form); // seja lá de onde vem esse $form;
} catch (Exception $e) {
    // tome as providências necessárias
}
  • http://stackoverflow.com/a/21099742/370290

  • Just because you can do it doesn’t mean you should do it.

  • Sure. Like smoking or eating meat.

Browser other questions tagged

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