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
OOP, classes and databases [message #169408] Fri, 10 September 2010 13:18 Go to previous message
Mattias Campe is currently offline  Mattias Campe
Messages: 7
Registered: September 2010
Karma:
Junior Member
Hello

I'm programming some classes that get their information from a MySQL
database. I started with mysql_query, function X () as constructor and
getValue and setValue.

I already found out that:
- PDO is better than mysql_query
- __construct is the way of making constructors
- __get and __set is the way for getting and setting properties


But I'm still kind of stuck with the 'best practices' with working
together with classes and databases. For now I have a class, let's say
'Person', that kind of works like this:

class Person {
private $name;
private $address;

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'];
}
}

public function __get ($var) {
switch ($var) {
case 'Name':
return $this->name;
break;
case 'address':
return $this->address;
break;
default:
break;
}
}

}

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?
- should I pass the database handler as a parameter or should I do it in
another way?


Would somebody happen to know a website that explains a little bit more
about OOP, classes and databases? I've already tried to do it myself,
but for now, I can't find the desired result.

Greetings
Mattias
[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:22:25 GMT 2024

Total time taken to generate the page: 0.04642 seconds