Re: OOP, classes and databases [message #169474 is a reply to message #169473] |
Tue, 14 September 2010 19:22 |
Thomas Mlynarczyk
Messages: 131 Registered: September 2010
Karma:
|
Senior Member |
|
|
Mattias Campe schrieb:
>> public static function getById(PDO $db, $id){
>> // ....
>> return new Person($row['id'], $row['name'], $row['adress']);
>> }
>
> How could I use this class? Because I would need to make a Person from
> the database, but first I would need to make a "random" Person, like:
>
> $oPerson = new Person("000","something that will be overwritten","blabla");
>
> $oPerson->getById($dbh,"245");
No. The crucial thing here is the "static" keyword: "public STATIC
function getById(...)" That means you don't need a Person instance to
access it, you simply write:
$oPerson = Person::getById( $dbh, '245' );
Greetings,
Thomas
--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
|
|
|