[SOLVED] Problem with OOP.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
damanno
Forum Newbie
Posts: 3
Joined: Wed Jul 22, 2009 7:37 am

[SOLVED] Problem with OOP.

Post 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.
Last edited by damanno on Wed Jul 22, 2009 3:41 pm, edited 1 time in total.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: Problem with OOP.

Post by spider.nick »

What does getArraySelect() return?

Nick
damanno
Forum Newbie
Posts: 3
Joined: Wed Jul 22, 2009 7:37 am

Re: Problem with OOP.

Post by damanno »

spider.nick wrote:What does getArraySelect() return?

Nick
getArraySelect() return an object input.

thx 4 answering
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: Problem with OOP.

Post 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
damanno
Forum Newbie
Posts: 3
Joined: Wed Jul 22, 2009 7:37 am

Re: Problem with OOP.

Post 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.
Post Reply