473,757 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('INSER T 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 1632
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($s tring){
if(!isset($this->statements[$string])) $this->statements[$string] =
$this->createStatemen t($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::getIn stance();
$this->statements['update'] = $db->getStatement(' UPDATE `cds` SET
`name` = :name,`length` = :length WHERE `id` = :id ');
}
$this->statements['update']->execute($thi s->properties);
}
}

Alternatively, you could prepare all statements in the __construct(),
which would save you some typing database::getIn stance() 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('INSER T 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
9992
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 MyMsg.Count Response.Write(MyMsg.Item(i)) next
8
10409
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 is not much help here either. Googling has given me a little help. This is my current understanding -- I would appreciate any comments or corrections... "" -- this means an empty string when applied to String data type, and also to Variant...
10
2856
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 have to add a datalist control. Using this datalist control I should be able to add edit, modify and cancel the items listed in this control. Here is how I designed. I used placeholder to add the controls dynamically to the page on the click...
6
2464
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 function exactly as outlined in the below link. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT02.asp
2
1666
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 showing all filetypes and icons. I have an object that has extension, desc (like Word Document) and then icon, which is nothing to start with. I have a collection object of these and when it is initialized it goes to
6
1461
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 example I found on the web. I would be most appreciative if someone could explain why this statement is necessary and what does it do: MyArt = New Art
2
1213
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 account number and retrieve an employee object called employee on computer 1 (user 1). The employee object getFirstName() returns "Michelle". 2. I repeat the process on computer 2 as above for the same account
2
1092
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 someone please help me understand why i would use a delegate in day to day use, over a function or sub? Thank you for your time! Ron
0
5573
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
53
8415
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 language="javascript" type="text/javascript">
0
10069
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.