PHP array of objects without access to get

4

I'm trying to access the getters that refer to an object, however this object is in an array, and by no means do you want to access them. Here is the class DAO (where you are giving the mess and I am doing test) and class VO:

DAO:

<?php
error_reporting(-1); 
ini_set('display_errors', 'On');
define('CONFIG', dirname(dirname(__DIR__)). '/config'. DIRECTORY_SEPARATOR);
define('MODEL_VO', dirname(dirname(__DIR__)). '/model/vo'. DIRECTORY_SEPARATOR);
include(CONFIG . 'conexao.php');
include(MODEL_VO . 'prioridade.php');

Class PrioridadeDao{
    private $conexao;
    private $prioridadeObj;
    private $prioridadeArrayObj = array();

    public function __construct(){
        $this->conexao = new Conexao();
    }

    public function teste(){
        echo "teste";
    }


    public function selectAll(){
        if(!$this->conexao->conecta()){
            exit;
        } else {
            $result = pg_query($this->conexao->conecta(), "SELECT * FROM prioridade");
            while ($consulta = @pg_fetch_array($result)){
                $this->prioridadeObj = new Prioridade($consulta['id'], $consulta['nivel'], $consulta['nome']);
                $this->prioridadeArrayObj[] = $this->prioridadeObj;
            }

            #### Aqui onde a coisa ocorre, no cast aparentemente está certo não consigo é acessar as coisas após ele
            #### por isso usei o var_dump para ver o que tem na saida.

            $this->prioridadeArrayObj = (object) $this->prioridadeArrayObj;
            var_dump($this->prioridadeArrayObj);
            $this->conexao->encerra();
        }
    }
}


Model VO:

<?php
Class Prioridade{
    private $id;
    private $nivel;
    private $nome;

    //public function __construct(){

    //}

    public function __construct($id, $nivel, $nome){
        $this->setId($id);
        $this->setNivel($nivel);
        $this->setNome($nome);
    }

    #GETTERS AND SETTERS
    public function setId($id){
        $this->id = $id;
    }

    public function setNivel($nivel){
        $this->nivel = $nivel;
    }

    public function setNome($nome){
        $this->nome = $nome;
    }

    public function getId(){
        return $this->id;
    }

    public function getNivel(){
        return $this->nivel;
    }

    public function getNome(){
        return $this->nome;
    }
}


As I'm doing in MVC I want to correctly use the interactions between classes. (If anyone has a legal mvc tutorial PHP thank you then the references I have found are making me confused). I have spent a lot of time programming with Java (Desktop) and to access the methods is super easy, in PHP I'm having a lot of difficulties. Another question, how do I do 2 methods constructors in a class that neither is possible in Java? Ex:
1 - public function __construct () {}
2 - public function __construct ($ param1, $ param2) {}

Thanks in advance. Home If I change the "var_dump" to "print_r" I have the following result:

stdClass Object ([0] => Priority Object ([id: Priority: private] => 1 [level: Priority: private] => 1 [name: Priority: private] = > => Priority Object ([id: Priority: private] => 2 [level: Priority: private] => 2 [name: Priority: private] = > 3 [level: Priority: private] => 3 [name: Priority: private] = > 4 [level: Priority: private] => 4 [name: Priority: private] => HIGH [4] = & : private] = > 5 [name: Priority: private] => VERY HIGH))

    
asked by anonymous 14.07.2015 / 05:08

1 answer

5

In the selectAll() method, it is not necessary to cast an array to object, the idea is the same as java create an array and then iterate over it, the difference is that you type it with generics something like Lista<Prioridade> and not in php, but the objects of that type are inside the array.

$this->prioridadeArrayObj = (object) $this->prioridadeArrayObj;

I suggest that you return the array at the end of the method in this way.

while ($consulta = pg_fetch_array($result)){
   $this->prioridadeObj = new Prioridade($consulta['id'], $consulta['nivel'], $consulta['nome']);
   $this->prioridadeArrayObj[] = $this->prioridadeObj;
}
$this->conexao->encerra();
return $this->prioridadeArrayObj;

After that get the return of selectAll() and make a foreach.

$dao = new PrioridadeDao();
$itens = $dao->selectAll();
foreach($itens as $item){
   $echo $item->getNivel() .' - '. $item->getNome();
}
  

Another question, how can I do 2 constructor methods in one   class that is not even possible in Java? > Ex:

     

1 - public function > __construct () {}

     

2 - public function __construct ($ param1, $ param2) {}

PHP does not support overload so it is not possible to have multiple constructors, however it is possible to have a function / constructor with a variable number of arguments.

In php5.6 you can use:

function funcao(...$param) {

In previous versions use the func_num_args function

Recommended reading

What is a builder for?

Can I create classes with two constructors?

How to access object properties with names like integers?

    
14.07.2015 / 05:47