show values of one method in another method with php oriented to objects

0

Good, I'm working with an object-oriented php, so I have one class and two methods, in the first method I have a foreach where I extract values from the database

public function ambitosController(){

        $respuesta = gestorOposicionesModels::ambitosModel("amb_oposiciones");

        foreach($respuesta as $row => $item){

        echo '<option value="'.$item["titulo"].'">'.$item["titulo"].'</option>';

        }
}

And for the other method where I would like to see what the aforementioned method brings, I am doing another foreach but in one part I would like you to show me the value of the first method.

I can not think of anything, I already tried invoking the method in the following way:

$categoria = self::categoriasController();

But it shows me the results outside of where I'd like you to show them to me ..

    
asked by Avancini1 18.10.2017 в 08:15
source

1 answer

0

I see you call categoriesController using self :: , invoking a method using self is done when you call a defined constant or a static method strong>. Otherwise you should call as follows:

$this->categoriasController();

In the case that this is not the problem, you should explain the problem a bit more or show the whole class to see what happens, since the problem is not very well understood " it shows me the results outside where I'd like you to show them to me ".

EDIT: Also comment, that a method should not end with echo (hence maybe it shows you the value where you do not want), the functions must end with a return , this does not mean that it will generate an error, but it is advisable. A well-done example of your method:

public function ambitosController(){
    $respuesta = gestorOposicionesModels::ambitosModel("amb_oposiciones");

    $res = '';
    foreach($respuesta as $row => $item){
        $res .= '<option value="'.$item["titulo"].'">'.$item["titulo"].'</option>';
    }

    return $res;
}

Greetings

    
answered by 18.10.2017 / 11:37
source