FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » OOP, classes and databases
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: OOP, classes and databases [message #169444 is a reply to message #169436] Mon, 13 September 2010 10:49 Go to previous messageGo to previous message
Piyush Gupta is currently offline  Piyush Gupta
Messages: 6
Registered: September 2010
Karma:
Junior Member
On Sep 13, 11:41 am, "Álvaro G. Vicario"
<alvaro.NOSPAMTH...@demogracia.com.invalid> wrote:
> El 10/09/2010 15:18, Mattias Campe escribió/wrote:
>
>> public function __construct ($id,$db) {
>> $query = ... the select query ...
>> $row = $db->query($query);
>> foreach ($db->query($query) as $row) {
>> $this->name = $row['name'];
>> $this->address = $row['address'];
>> }
>> }
> [...]
>> But I have the feeling that this isn't a 'best practice':
>> - I can't use this class without database
>> - should I put the select query and getting the data in a seperate
>> function, outside the constructor?
>
> You are right about this gotcha. One approach is to make your
> constructor and most other methods DB agnostic:
>
> public function __construct($id, $name, $address){
>         // ...
>
> }
>
> ... and then build a set of static methods to perform database reads:
>
> public static function getById(PDO $db, $id){
>         // ....
>         return new Person($row['id'], $row['name'], $row['adress']);
>
> }
>> - should I pass the database handler as a parameter or should I do it in
>> another way?
>
> When I've done it that way I've found it quite practical.
>
> --
> --http://alvaro.es- Álvaro G. Vicario - Burgos, Spain
> -- Mi sitio sobre programación web:http://borrame.com
> -- Mi web de humor satinado:http://www.demogracia.com
> --

Hi ,

We at www.rntlabs.com [RNTLABS Software Solutions] have exciting
openings for the below

1/ PHP
2/ Designers
3/ Android
4/ iPhone
5/ QA
6/ ROR

Please apply at hr(at)rntlabs(dot)com ASAP .

Thanks

Piyush Gupta
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Best PHP way to connect to a DB across multiple pages ?
Next Topic: When do I use {}?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 19:58:34 GMT 2024

Total time taken to generate the page: 0.03768 seconds