Home > Archive > PHP Programming > February 2007 > Help understanding object-oriented approach
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Help understanding object-oriented approach
|
|
| Andrew Taylor 2007-02-22, 7:00 pm |
| Hi,
I've been using PHP for a long time, I have designed and developed a
number of mid-range systems. I've always used a procedural approach. I
fully understand the concept of OO, I know all the basics and I know
how to code OO. What I'm having problems with is understanding how to
design/architect projects using OO.
Can anyone reccomend any good online tutorials / online learning /
video tutorials which deal with these subjects specifically in PHP. I
have a number of the class OO books which help me understand the
principals, but, not how to leverage those principals in to system
design.
Also, I am trying to learn how to do this myself, this is not just a
lazy call for the answer, perhaps somebody could answer this which
might help me on my way as well as the above request for further
information.
One thing which confuses me is how to use databases in the OO approach.
Imagine we have a system which deals with music CDs:
class MusicCD {
private $title;
private $artist;
function __constructor($title, $artist) {
$this->title = $title;
$this->artist = $artist;
}
function displayCD() {
echo $this->title.' '.$this->artist;
}
}
Now, we want to save this information in to a database, what is the
correct way (best practice) for doing this; is it to create a saveCD
method in my musicCD class? Should this method expect a database object
to be passed in, or, should it call static methods of another class?
function saveCD(Database $databaseObject) {
$databaseObject->query('INSERT INTO...
}
function saveCD() {
mysql_connect(...)
}
Surely in the OO concept the MusicCD class doesn't need to know about
my table structure etc, would this be a case for a MusicCDDatabase
class which knows about MusicCD and DB?
Just to re-itterate, I know how to code in PHP, I know how to use OO
syntax, I'm just having problems designing my system.
Thanks for reading a long post, and thanks in advance for any help.
Andrew
| |
|
| On Thu, 22 Feb 2007 17:44:47 +0100, Andrew <Taylor> wrote:
> Hi,
>
> I've been using PHP for a long time, I have designed and developed a =
> number of mid-range systems. I've always used a procedural approach. I=
=
> fully understand the concept of OO, I know all the basics and I know h=
ow =
> to code OO. What I'm having problems with is understanding how to =
> design/architect projects using OO.
>
> Can anyone reccomend any good online tutorials / online learning / vid=
eo =
> tutorials which deal with these subjects specifically in PHP. I have a=
=
> number of the class OO books which help me understand the principals, =
=
> but, not how to leverage those principals in to system design.
Online I haven't found resources to my liking. I have thoroughly enjoyed=
=
the following book however: "PHP 5 Objects, Patterns, and Practice", by =
=
Matt Zandstra (#ISBN:1-59059-380-4).
> Now, we want to save this information in to a database, what is the =
> correct way (best practice) for doing this; is it to create a saveCD =
> method in my musicCD class? Should this method expect a database objec=
t =
> to be passed in, or, should it call static methods of another class?
I usually define either a Database as a singleton and/or save prepared =
=
statement resources as a private variable.
So it would be something like this (pseudocode):
class database{
private static $instance;
private $statements =3D array();
private function __construct(){
//some connection methods etc.
}
public static function getInstance(){
if(empty(self::$instance) self::$instance =3D new database;
return self::$instance;
}
public function getStatement($string){
if(!isset($this->statements[$string])) $this->statements[$string] =3D =
=
$this->createStatement($string);
return $this->statements[$string]
}
private function createStatement($string){
//code to get the prepared statement
return $statement;
}
}
class musicCD{
private $statements =3D array();
private $properties =3D array();
...
public function updateCD(){
if(!isset($this->statements['update'])){
$db =3D database::getInstance();
$this->statements['update'] =3D $db->getStatement('UPDATE `cds` SET =
`name` =3D :name,`length` =3D :length WHERE `id` =3D :id ');
}
$this->statements['update']->execute($this->properties);
}
}
Alternatively, you could prepare all statements in the __construct(), =
which would save you some typing database::getInstance() in every functi=
on =
that has to do something with the db, or you could declare your own =
private function int he class which does this. Might be some overhead =
preparing statements you're not going to need though.
-- =
Rik Wasmus
| |
| Dikkie Dik 2007-02-22, 7:00 pm |
| <snip>
> One thing which confuses me is how to use databases in the OO approach.
> Imagine we have a system which deals with music CDs:
>
> class MusicCD {
> private $title;
> private $artist;
> function __constructor($title, $artist) {
> $this->title = $title;
> $this->artist = $artist;
> function displayCD() {
> echo $this->title.' '.$this->artist;
> }
> }
Nothing wrong with that. For storing it in a database, you may add an
"ID" property. Also note that this object is immutable: it cannot be
changed after construction. This could be exactly what you need if you
only want to show CDs from a database table, but if you want to edit the
fields, you'd have to add getters and setters to the class.
> Now, we want to save this information in to a database, what is the
> correct way (best practice) for doing this; is it to create a saveCD
> method in my musicCD class? Should this method expect a database object
> to be passed in, or, should it call static methods of another class?
I usually work with "record" classes (like your MusicCD class) and with
collection classes that represent tables rather than records. My first
step to database code is to put the database handling stuff in the
collection classes. These can be lazy (see
http://www.w-p.dds.nl/article/wrtabrec.htm#laziness ). Your collection
class can now feature a Store(MusicCd $CD) method that checks the
properties of the record. If its ID is an integer, it builds up an
UPDATE query. If it is NULL, it is a new record, so an INSERT query has
to be built and the ID property has to be set to the generated ID after
storing.
Next step is to note which database code is the same or similar in all
these classes and extract that to either a superclass or a helper class,
or both.
<snip>
> Surely in the OO concept the MusicCD class doesn't need to know about my
> table structure etc, would this be a case for a MusicCDDatabase class
> which knows about MusicCD and DB?
You are getting the hang of it. Database handling is not the
reponsibility of a record. It may be the responsibility of a collection,
and the collection may delegate this responsibility to a more
specialized class.
Best regards.
| |
| OmegaJunior 2007-02-22, 7:00 pm |
| On Thu, 22 Feb 2007 17:44:47 +0100, Andrew <Taylor> wrote:
> Hi,
>
> I've been using PHP for a long time, I have designed and developed a =
> number of mid-range systems. I've always used a procedural approach. I=
=
> fully understand the concept of OO, I know all the basics and I know h=
ow =
> to code OO. What I'm having problems with is understanding how to =
> design/architect projects using OO.
>
> Can anyone reccomend any good online tutorials / online learning / vid=
eo =
> tutorials which deal with these subjects specifically in PHP. I have a=
=
> number of the class OO books which help me understand the principals, =
=
> but, not how to leverage those principals in to system design.
>
> Also, I am trying to learn how to do this myself, this is not just a =
> lazy call for the answer, perhaps somebody could answer this which mig=
ht =
> help me on my way as well as the above request for further information=
..
>
> One thing which confuses me is how to use databases in the OO approach=
.. =
> Imagine we have a system which deals with music CDs:
>
> class MusicCD {
>
> private $title;
> private $artist;
>
> function __constructor($title, $artist) {
>
> $this->title =3D $title;
> $this->artist =3D $artist;
> }
>
> function displayCD() {
>
> echo $this->title.' '.$this->artist;
> }
> }
>
> Now, we want to save this information in to a database, what is the =
> correct way (best practice) for doing this; is it to create a saveCD =
> method in my musicCD class? Should this method expect a database objec=
t =
> to be passed in, or, should it call static methods of another class?
>
> function saveCD(Database $databaseObject) {
>
> $databaseObject->query('INSERT INTO...
> }
>
> function saveCD() {
>
> mysql_connect(...)
> }
>
> Surely in the OO concept the MusicCD class doesn't need to know about =
my =
> table structure etc, would this be a case for a MusicCDDatabase class =
=
> which knows about MusicCD and DB?
>
> Just to re-itterate, I know how to code in PHP, I know how to use OO =
> syntax, I'm just having problems designing my system.
>
> Thanks for reading a long post, and thanks in advance for any help.
>
> Andrew
>
Interesting question, Andrew! (And I'm not just saying that because we'r=
e =
name-sakes ;) )
I was discussing a similar topic with one of my colleagues at work:
where does the user interface come in?
And we found two approaches:
1) The interface is defined separately, either using O.O. techniques or =
=
not, and gets told about the O.O. system in design time. Then the user =
chooses some functionality, which in turn invokes the O.O. instances.
2) The O.O. system is told that for some methods it needs a user =
interface, thus the instance constructors accept parameters that indicat=
e =
which parts of an existing user interface belongs to which method. Then =
=
when the user chooses some functionality, it's the O.O. instance that =
invokes the user interface.
Something similar can be conceived with database access:
The class needs to know when it needs to save anything... but not =
necessarily where or how. So you could design a helper class and offload=
=
the actual database interaction to an instance thereof, or you could hav=
e =
your MusicCD class inherit and extend a database-capable class. That way=
=
you could derive a MusicCD class from an OracleSQL class, or from a MySQ=
L =
class, or from a Text class... or all of them combined, choosing which t=
o =
use during run-time, based on some configuration.
-- =
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
| |
| Andrew Taylor 2007-02-23, 8:00 am |
| On 2007-02-22 16:44:47 +0000, Andrew Taylor said:
> Hi,
>
> I've been using PHP for a long time, I have designed and developed a
> number of mid-range systems. I've always used a procedural approach. I
> fully understand the concept of OO, I know all the basics and I know
> how to code OO. What I'm having problems with is understanding how to
> design/architect projects using OO.
> <snip rest of my question>
<snip loads of really good answers>
Thanks very much, all three of those responses so far have been perfect.
I have the Matt Zandstra book here and I agree it really is brilliant,
but, it's quite a high concept and when I was reading it, I was
struggling to apply it. I thought if I could watch a lecture or some
training I might be able to go back to the book with a little more
ammunition.
I'm really glad I posted this question, I haven't used usenet since the
mid 90's and these responses prove that there is still a really good
source of knowledge once you get passed the trolls.
Thanks again and I hope this conversation continues further, judging by
OmegaJunior's response; there are more than just me sitting pondering
best practice.
Andrew
|
|
|
|
|