Re: niewbie - call methods from another class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Apr 19, 2009, at 9:43 AM, MEM wrote:

Hello, I have something like this:

$stmt = $this->_dbh->prepare("INSERT INTO DOG (name_dog, race_dog, id_vet)
VALUES (?, ?, ?)");

            $stmt->bindParam(1, $this->getNameDog() );
            $stmt->bindParam(2, $this->getRaceDog());
            $stmt->bindParam(3, ????????????????????);


            $stmt->execute();
      }

To make this insert function work, I need to fill the id_vet database column
with some values.
I cannot use $this->getIdVet() because this method is not in dog class, is
on the veterinary class.
So my question is:
How can I access the getIdVet() that is on the veterinary class to put it on
the insert dog method?


Thanks a lot,
Márcio

Try this....

<?php
class veterinary {
    public function getIdVet () {
        return $this->id;
    }
}

class dog {
    public function add () {
        $vet = new veterinary ();

$stmt = $this->_dbh->prepare("INSERT INTO DOG (name_dog, race_dog, id_vet) VALUES (?, ?, ?)");

        $stmt->bindParam(1, $this->getNameDog());
        $stmt->bindParam(2, $this->getRaceDog());
        $stmt->bindParam(3, $vet->getIdVet());
        $stmt->execute();
    }
}
?>

Of course here you will have to determine how the vet id is set. The method getIdVet has a redundant name. Since it's already in the veterinary class, just use getId() or id(). However, you'll probably want to change the above implementation to not create the veterinary in the add() method. You'll probably want to create it in the dog constructor...

<?php
class dog {
    public function __construct ($vetId) {
        $this->vet = new veterinary ($vetId);
    }
    ...
}
?>

And use $this->vet instead of $vet in the dog->add() method. Hope that helps. Do a little reading to try to understand using objects a little bit more.....

http://php.net/object

~Philip


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux