6
I’m trying to access the getters which refer to an object, but this object is in an array, and by no means can I access them. Follow the DAO class (where the mess is taking place and I’m taking the test) and the VO class:
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 am doing in MVC I want to use the interactions between the classes correctly. (If someone has a nice tutorial of PHP mvc thank you because the references I have found are confusing me). I spent a lot of time programming with Java (Desktop) and to access the methods is super easy, already in PHP I’m having many difficulties.
Another question, can I do 2 constructor methods in a class that is not even possible in Java? Ex:
1 - public Function __Construct(){}
2 - public Function __Construct($param1, $param2){}
Thank you in advance.
If I replace "var_dump" with "print_r" I have the following result:
stdClass Object ( [0] => Priority Object ( [id:Priority:private] => 1 [level:Priority:private] => 1 [name:Priority:private] => VERY LOW ) [1] => Priority Object ( [id:Priority:private] => 2 [level:Priority:private] => 2 [name:Priority:private] => LOW ) [2] => Priority Object ( [id:Priority:private] => 3 [level:Priority:private] => 3 [name:Priority:private] => NORMAL ) [3] => Priority Object ( [id:Priority:private] => 4 [level:Priority:private] => 4 [name:Priority:private] => HIGH ) [4] => Priority Object ( [id:Priority:private] => 5 [level:Priority:private] => 5 [name:Priority:private] => VERY HIGH ) )
And what came in var_dump? Have you tried print_r()? Remove@ from this line: @pg_fetch_array($result)... to handle errors.
– Sr. André Baill
I didn’t understand the doubt :\
– rray
@Andrébaill edited the post to show you what appears on print_r. A and apart from @ no mistake occurs, there is all right what I ask is in sequence.
– Luan G. C. Rodrigues
you want to access an object within the array?
– rray
@rray yes want to access the get methods of this object, in the priority class that posted have the get methods, I want through DAO rescue these values of the objects through the GET methods, because I do not want the attributes to be public, to access them directly.
– Luan G. C. Rodrigues
Tried somehow to use a foreach ($var as $arr) to access them?
– Sr. André Baill
So that the cast for Object?
– bfavaretto
@Andrébaill didn’t try no, because I don’t know what the sequence of it looks like. Would it look like: $arr->getNome(); ??? or do I have to create a new variable to receive this value? Our guy this Object business in php is very confusing :(
– Luan G. C. Rodrigues
I’m not sure what you’re getting at... But I would try to use foreach($this->priorityArrayObj as $object){ echo $object->field; } - I’m just not sure it will work in your case, it wouldn’t hurt to try! :)
– Sr. André Baill
@bfavaretto I saw in a post to convert, I thought saying that the type was Object could access the methods somehow. But on the Internet I only get things with direct access nothing with private attribute. I must be searching wrong only can.
– Luan G. C. Rodrigues
@Andrébaill you got what I wanted to understand, I was closer than expected, I had not found something that had helped me (only worse rsrs). This your previous comment of foreach helped me a lot, I made a test and this way and I was able to access the get method, which looks for what I need without directly accessing the object attribute, even because it is as private. I’ll post the code.
– Luan G. C. Rodrigues
Great. I’ve had this problem many times and solved it using foreach... But there’s also the option @rray posted, which is almost my idea, only organized :) rsrs
– Sr. André Baill
Thank you @Andrébaill your commitment and dedication were very helpful in solving my problem.
– Luan G. C. Rodrigues
For nothing! A disposition.. Whenever you have doubts.. Hug.
– Sr. André Baill