Page 1 of 1

[SOLVED] Problem with OOP.

Posted: Wed Jul 22, 2009 7:57 am
by damanno
Hi guys, 1st post here, im a little noob with oop on php, but not too much.

I´m using an object, and writing in a array inside other object. The problem is, when i have more than 1 call to object input without renew, even using an array inside the object who is inheriting the object, it just maintein the last value i insert inside it.

Code: Select all

 
private input = array();
   
public function setInput($input) {
        $this->input[] = $input;
    }
 
i am using like this

Code: Select all

 
$input = new input();
$form->setInput($input->getArraySelect('cliente', $clientes, $cliente, $erro['cliente']));
$form->setInput($input->getArraySelect('evento', $eventos, $evento, $erro['evento']));
the output for example is:
evento
evento

I need the output like this.
cliente
evento

I already know how it works ok, if i use the code below, but i do not want to create hundreds of times the object input.

Code: Select all

 
$input = new input();
$form->setInput($input->getArraySelect('cliente', $clientes, $cliente, $erro['cliente']));
$input = new input();
$form->setInput($input->getArraySelect('evento', $eventos, $evento, $erro['evento']));
 
Here´s my problem. There´s anyway i could do something like this on php?

Code: Select all

 
class object () {
//this function might receive a new object 
function setObject($object) {
        if (is_object($input) && $this != $input) {
        $this = $input;
        }
}
 
or to instantiate a new object like this

Code: Select all

 
$form->setInput(new input()->getArraySelect('cliente', $clientes, $cliente, $erro['cliente']));
$form->setInput(new input()->getArraySelect('evento', $eventos, $evento, $erro['evento']));
 
Some like that could solve my problem, but like what i'm doing, php crashes miserably.

If anyone of you guys have a good recomendation of php opp lecture, i would be glad 2 :)

Thanx 4 ur attention.

Re: Problem with OOP.

Posted: Wed Jul 22, 2009 8:48 am
by spider.nick
What does getArraySelect() return?

Nick

Re: Problem with OOP.

Posted: Wed Jul 22, 2009 9:00 am
by damanno
spider.nick wrote:What does getArraySelect() return?

Nick
getArraySelect() return an object input.

thx 4 answering

Re: Problem with OOP.

Posted: Wed Jul 22, 2009 10:10 am
by spider.nick
damanno wrote:
spider.nick wrote:What does getArraySelect() return?

Nick
getArraySelect() return an object input.

thx 4 answering

Code: Select all

 
private input = array();
   
public function setInput($input) {
        $this->input[] = $input;
}
 
So, getArraySelect() returns an object, which is then passed to setInput. In setInput(), it moves the object into the array. Is there another function that is outputting something to the screen?

Nick

Re: Problem with OOP.

Posted: Wed Jul 22, 2009 3:28 pm
by damanno
The only output is $form->getFormulario();

Anyway Nick, i solved the problem like this:

Code: Select all

 
    public static function getInstance() {
        return new self();
    }
 
I have include the code above. And then, i use it like this.

Code: Select all

 
$field = array('cliente','evento','usuario','senha','acao','logar');
$data = array('clientes','eventos','usuario','senha','acao','logar');
$form = new formulario();
$i = 0;
 
$form->setInput(input::getInstance()->getArraySelect($field[$i], ${$data[$i]} , ${$field[$i]}, $erro[$field[$i++]]));
$form->setInput(input::getInstance()->getArraySelect($field[$i], ${$data[$i]} , ${$field[$i]}, $erro[$field[$i++]]));
$form->setInput(input::getInstance()->Texto($field[$i], ${$data[$i]} , ${$field[$i]}, $erro[$field[$i++]]));
 
Like this, i do not need to do the "new", i can evoke the method direct. Not really direct, the new object is created, but the way i wanted :)

Thanx for ur answer and attention.