473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help understanding object-oriented approach

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

Feb 22 '07 #1
4 1608
Rik
On Thu, 22 Feb 2007 17:44:47 +0100, Andrew <Taylorwrote:
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.
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 object
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 = array();
private function __construct(){
//some connection methods etc.
}
public static function getInstance(){
if(empty(self::$instance) self::$instance = new database;
return self::$instance;
}
public function getStatement($string){
if(!isset($this->statements[$string])) $this->statements[$string] =
$this->createStatement($string);
return $this->statements[$string]
}
private function createStatement($string){
//code to get the prepared statement
return $statement;
}
}
class musicCD{
private $statements = array();
private $properties = array();
...
public function updateCD(){
if(!isset($this->statements['update'])){
$db = database::getInstance();
$this->statements['update'] = $db->getStatement('UPDATE `cds` SET
`name` = :name,`length` = :length WHERE `id` = :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 function
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
Feb 22 '07 #2
<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.
Feb 22 '07 #3
On Thu, 22 Feb 2007 17:44:47 +0100, Andrew <Taylorwrote:
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
Interesting question, Andrew! (And I'm not just saying that because we're
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 indicate
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 have
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 MySQL
class, or from a Text class... or all of them combined, choosing which to
use during run-time, based on some configuration.

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Feb 22 '07 #4
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
Feb 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: What-a-Tool | last post by:
Dim MyMsg Set MyMsg = server.createObject("Scripting.Dictionary") MyMsg.Add "KeyVal1", "My Message1" MyMsg.Add "KeyVal2", "My Message2" MyMsg.Add "KeyVal3", "My Message3" for i = 1 To...
8
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have...
10
by: Bharat | last post by:
Hi Folks, Suppose I have two link button on a page (say lnkBtn1 and lnkBtn2). On the click event of the lnkbtn1 I have to add a dynamically created control. And On the click event of the lnkBtn2 I...
6
by: varkey.mathew | last post by:
Dear all, Bear with me, a poor newbie(atleast in AD).. I have to authenticate a user ID and password for a user as a valid Active Directory user or not. I have created the IsAuthenticated...
2
by: SStory | last post by:
Here is the situation. I want to display Icons, Type of file etc from a file extension. Upon initial program load I may only need icons for certain files. But other operations will require...
6
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB...
2
by: Mark | last post by:
Hi, I don't have a code problem. I have an understanding problem. This is a made up situation that mimics my real situation but it is easier to understand. 1. I call a web method with an...
2
by: RSH | last post by:
I have been looking at delegates lately. I have seen several articles explaining them but I'm having a hard time understanding why I would use a delegate over a traditional function or sub. Could...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.