Reputation: 1064
I am writing a new website that uses PHP and MySQL. I am trying to do this using OO and would welcome a bit of advice.
I know that I should structure the class as follows but what I am not sure about is whether I should create separate methods for accessing the database to insert, update etc. the name.
Does it matter? Should I have separate methods or can I just roll the code into set_name and get_name?
<?php
class person {
var $name;
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>
Upvotes: 0
Views: 166
Reputation: 197757
You basically should not care about your concrete database. The Person
class should not care at all how or even if ever it is stored somewhere.
Just keep the things apart. E.g. for starters you create a ObjectMockStorage
class that has a store
and retrieve
method.
These methods can decide by the object you pass in what should happen with that object.
You can then just create your application and until you actually need storage (maybe not even MySQL who knows already today?), you can change the type of the Storage
to something different with the same interface.
This also helps you to get your application more quickly running and actually not wasting time in caring about things you don't yet need to care about.
Upvotes: 0
Reputation: 2385
You might be interested in Object-relation-mapping and/or specifically the Active Record Pattern as a starter. See also this example for a implementation.
Upvotes: 2
Reputation: 1730
Can you use php5?
Look in the direction of ready-made solutions, for example Doctrine-ORM or other ORM engine.
Upvotes: 0