473,549 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object Oriented Approach to PHP programing and MySQL database

Hello everybody,

I am new to PHP and working on extending my knowledge of OOP. The
posts in this group concerned with whether or not to use an OO
approach when programming in PHP is not what I want to discuss in this
post. Rather I would like to discuss the best way to program the
following problem:

I have a MySQL Database which for example exists of phonenumbers. The
table might look something like this.

+---+---------------+------+
|ID | PHONENUMBER | FLAGS|
+---+---------------+------+
|001| 612341234| FALSE|
+---+---------------+------+
|002| 612341235| TRUE |
+---+---------------+------+
|...| ...| FALSE|
+---+---------------+------+
|00N| 612341238| FALSE|
+---+---------------+------+

An object representation in PHP might be the object PhoneNumber. The
object PhoneNumber would be extended from a general class called
Entity. The class Entity allows for easy creation of new database
objects. Say I would like an EmailAddress object, I would simply
create the MySQL table, extend the Entity class with an EmailAddress
class and populate the SQL variables with the correct SQL statements.

Until this far everything is ok. But what if I would like an extended
version of the objects PhoneNumber or Emailaddress? Say for example
that I would like an object called MobilePhoneNumb er. The only
difference with the normal PhoneNumberClas s is that it is of the type
MobilePhoneNumb er (usage: A function only accepting MobilePhoneNumb er
Objects for sending text messages). I would like to create a MySQL
table that consists of mobilephonenume rid's and phonenumberid's .

What would be the best way, rewriting the internals of the Entity
class and just fetch the data from MySQL right away or writing new
internals for the MobilePhoneNumb ers class which create a
phoneNumberObje ct if it does not exist?

Hope that my problem and questions are clear, if not please ask for
more info.

Thanks in advance

Marijn

Jun 3 '07 #1
5 2707
On 03.06.2007 21:33 Marijn wrote:
Hello everybody,

I am new to PHP and working on extending my knowledge of OOP. The
posts in this group concerned with whether or not to use an OO
approach when programming in PHP is not what I want to discuss in this
post. Rather I would like to discuss the best way to program the
following problem:

I have a MySQL Database which for example exists of phonenumbers. The
table might look something like this.

+---+---------------+------+
|ID | PHONENUMBER | FLAGS|
+---+---------------+------+
|001| 612341234| FALSE|
+---+---------------+------+
|002| 612341235| TRUE |
+---+---------------+------+
|...| ...| FALSE|
+---+---------------+------+
|00N| 612341238| FALSE|
+---+---------------+------+

An object representation in PHP might be the object PhoneNumber. The
object PhoneNumber would be extended from a general class called
Entity. The class Entity allows for easy creation of new database
objects. Say I would like an EmailAddress object, I would simply
create the MySQL table, extend the Entity class with an EmailAddress
class and populate the SQL variables with the correct SQL statements.

Until this far everything is ok. But what if I would like an extended
version of the objects PhoneNumber or Emailaddress? Say for example
that I would like an object called MobilePhoneNumb er. The only
difference with the normal PhoneNumberClas s is that it is of the type
MobilePhoneNumb er (usage: A function only accepting MobilePhoneNumb er
Objects for sending text messages). I would like to create a MySQL
table that consists of mobilephonenume rid's and phonenumberid's .

What would be the best way, rewriting the internals of the Entity
class and just fetch the data from MySQL right away or writing new
internals for the MobilePhoneNumb ers class which create a
phoneNumberObje ct if it does not exist?

Hope that my problem and questions are clear, if not please ask for
more info.

Thanks in advance

Marijn
Hi Marijn

your problem is clear and well-known, particularly PoEAA book discusses
diverse approaches in detail (see e.g. "Single Table Inheritance" and
other ORM patterns).

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 4 '07 #2
On Jun 4, 10:05 am, gosha bine <stereof...@gma il.comwrote:
On 03.06.2007 21:33 Marijn wrote:
Hello everybody,
I am new to PHP and working on extending my knowledge of OOP. The
posts in this group concerned with whether or not to use an OO
approach when programming in PHP is not what I want to discuss in this
post. Rather I would like to discuss the best way to program the
following problem:
I have a MySQL Database which for example exists of phonenumbers. The
table might look something like this.
+---+---------------+------+
|ID | PHONENUMBER | FLAGS|
+---+---------------+------+
|001| 612341234| FALSE|
+---+---------------+------+
|002| 612341235| TRUE |
+---+---------------+------+
|...| ...| FALSE|
+---+---------------+------+
|00N| 612341238| FALSE|
+---+---------------+------+
An object representation in PHP might be the object PhoneNumber. The
object PhoneNumber would be extended from a general class called
Entity. The class Entity allows for easy creation of new database
objects. Say I would like an EmailAddress object, I would simply
create the MySQL table, extend the Entity class with an EmailAddress
class and populate the SQL variables with the correct SQL statements.
Until this far everything is ok. But what if I would like an extended
version of the objects PhoneNumber or Emailaddress? Say for example
that I would like an object called MobilePhoneNumb er. The only
difference with the normal PhoneNumberClas s is that it is of the type
MobilePhoneNumb er (usage: A function only accepting MobilePhoneNumb er
Objects for sending text messages). I would like to create a MySQL
table that consists of mobilephonenume rid's and phonenumberid's .
What would be the best way, rewriting the internals of the Entity
class and just fetch the data from MySQL right away or writing new
internals for the MobilePhoneNumb ers class which create a
phoneNumberObje ct if it does not exist?
Hope that my problem and questions are clear, if not please ask for
more info.
Thanks in advance
Marijn

Hi Marijn

your problem is clear and well-known, particularly PoEAA book discusses
diverse approaches in detail (see e.g. "Single Table Inheritance" and
other ORM patterns).

--
gosha bine

extended php parser ~http://code.google.com/p/pihipi
blok ~http://www.tagarga.com/blok
Thanks for your respons Gosha. Do you have the isbn # of the book? Is
there other (online) literature you would advise me to read? What is
your experience with this problem. What I'm concerned with most is
that if I would want to extend a class which has rewritten innerworks
I have to rewrite them again and again every time I extend. This would
probably result in more coding than less, hence a bit past it's goal.
While rewriting the innerworks every time might be a more optimal
concerning speed...

Thanks,

Marijn

Jun 4 '07 #3
On 04.06.2007 13:59 Marijn wrote:
On Jun 4, 10:05 am, gosha bine <stereof...@gma il.comwrote:
>On 03.06.2007 21:33 Marijn wrote:
>>Hello everybody,
I am new to PHP and working on extending my knowledge of OOP. The
posts in this group concerned with whether or not to use an OO
approach when programming in PHP is not what I want to discuss in this
post. Rather I would like to discuss the best way to program the
following problem:
I have a MySQL Database which for example exists of phonenumbers. The
table might look something like this.
+---+---------------+------+
|ID | PHONENUMBER | FLAGS|
+---+---------------+------+
|001| 612341234| FALSE|
+---+---------------+------+
|002| 612341235| TRUE |
+---+---------------+------+
|...| ...| FALSE|
+---+---------------+------+
|00N| 612341238| FALSE|
+---+---------------+------+
An object representation in PHP might be the object PhoneNumber. The
object PhoneNumber would be extended from a general class called
Entity. The class Entity allows for easy creation of new database
objects. Say I would like an EmailAddress object, I would simply
create the MySQL table, extend the Entity class with an EmailAddress
class and populate the SQL variables with the correct SQL statements.
Until this far everything is ok. But what if I would like an extended
version of the objects PhoneNumber or Emailaddress? Say for example
that I would like an object called MobilePhoneNumb er. The only
difference with the normal PhoneNumberClas s is that it is of the type
MobilePhoneNu mber (usage: A function only accepting MobilePhoneNumb er
Objects for sending text messages). I would like to create a MySQL
table that consists of mobilephonenume rid's and phonenumberid's .
What would be the best way, rewriting the internals of the Entity
class and just fetch the data from MySQL right away or writing new
internals for the MobilePhoneNumb ers class which create a
phoneNumberOb ject if it does not exist?
Hope that my problem and questions are clear, if not please ask for
more info.
Thanks in advance
Marijn
Hi Marijn

your problem is clear and well-known, particularly PoEAA book discusses
diverse approaches in detail (see e.g. "Single Table Inheritance" and
other ORM patterns).

--
gosha bine

extended php parser ~http://code.google.com/p/pihipi
blok ~http://www.tagarga.com/blok

Thanks for your respons Gosha. Do you have the isbn # of the book? Is
there other (online) literature you would advise me to read? What is
your experience with this problem. What I'm concerned with most is
that if I would want to extend a class which has rewritten innerworks
I have to rewrite them again and again every time I extend. This would
probably result in more coding than less, hence a bit past it's goal.
While rewriting the innerworks every time might be a more optimal
concerning speed...

Thanks,

Marijn
The official PoEAA site is http://www.martinfowler.com/books.html#eaa
There's also small patterns catalog on the site.

In my personal experience, I tend to use Table Gateways rather than
diverse ORM flavors because it's much simpler and uses aggregation
instead of inheritance.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 4 '07 #4
On Jun 4, 2:39 pm, gosha bine <stereof...@gma il.comwrote:
On 04.06.2007 13:59 Marijn wrote:
On Jun 4, 10:05 am, gosha bine <stereof...@gma il.comwrote:
On 03.06.2007 21:33 Marijn wrote:
>Hello everybody,
I am new to PHP and working on extending my knowledge of OOP. The
posts in this group concerned with whether or not to use an OO
approach when programming in PHP is not what I want to discuss in this
post. Rather I would like to discuss the best way to program the
following problem:
I have a MySQL Database which for example exists of phonenumbers. The
table might look something like this.
+---+---------------+------+
|ID | PHONENUMBER | FLAGS|
+---+---------------+------+
|001| 612341234| FALSE|
+---+---------------+------+
|002| 612341235| TRUE |
+---+---------------+------+
|...| ...| FALSE|
+---+---------------+------+
|00N| 612341238| FALSE|
+---+---------------+------+
An object representation in PHP might be the object PhoneNumber. The
object PhoneNumber would be extended from a general class called
Entity. The class Entity allows for easy creation of new database
objects. Say I would like an EmailAddress object, I would simply
create the MySQL table, extend the Entity class with an EmailAddress
class and populate the SQL variables with the correct SQL statements.
Until this far everything is ok. But what if I would like an extended
version of the objects PhoneNumber or Emailaddress? Say for example
that I would like an object called MobilePhoneNumb er. The only
difference with the normal PhoneNumberClas s is that it is of the type
MobilePhoneNum ber (usage: A function only accepting MobilePhoneNumb er
Objects for sending text messages). I would like to create a MySQL
table that consists of mobilephonenume rid's and phonenumberid's .
What would be the best way, rewriting the internals of the Entity
class and just fetch the data from MySQL right away or writing new
internals for the MobilePhoneNumb ers class which create a
phoneNumberObj ect if it does not exist?
Hope that my problem and questions are clear, if not please ask for
more info.
Thanks in advance
Marijn
Hi Marijn
your problem is clear and well-known, particularly PoEAA book discusses
diverse approaches in detail (see e.g. "Single Table Inheritance" and
other ORM patterns).
--
gosha bine
extended php parser ~http://code.google.com/p/pihipi
blok ~http://www.tagarga.com/blok
Thanks for your respons Gosha. Do you have the isbn # of the book? Is
there other (online) literature you would advise me to read? What is
your experience with this problem. What I'm concerned with most is
that if I would want to extend a class which has rewritten innerworks
I have to rewrite them again and again every time I extend. This would
probably result in more coding than less, hence a bit past it's goal.
While rewriting the innerworks every time might be a more optimal
concerning speed...
Thanks,
Marijn

The official PoEAA site ishttp://www.martinfowle r.com/books.html#eaa
There's also small patterns catalog on the site.

In my personal experience, I tend to use Table Gateways rather than
diverse ORM flavors because it's much simpler and uses aggregation
instead of inheritance.

--
gosha bine

extended php parser ~http://code.google.com/p/pihipi
blok ~http://www.tagarga.com/blok
Already ordered the book on Amazon. I was wondering if anyone could
give me a small introduction to this problem in the post. Would like
to start thinking about the problem now instead of 48 hours when the
book arrives.

Thanks Marijn

Jun 4 '07 #5
On Jun 4, 11:06 am, Marijn <marijn.huizend v...@gmail.comw rote:
On Jun 4, 2:39 pm, gosha bine <stereof...@gma il.comwrote:
On 04.06.2007 13:59 Marijn wrote:
On Jun 4, 10:05 am, gosha bine <stereof...@gma il.comwrote:
>On 03.06.2007 21:33 Marijn wrote:
>>Hello everybody,
>>I am new to PHP and working on extending my knowledge of OOP. The
>>posts in this group concerned with whether or not to use an OO
>>approach when programming in PHP is not what I want to discuss in this
>>post. Rather I would like to discuss the best way to program the
>>following problem:
>>I have a MySQL Database which for example exists of phonenumbers. The
>>table might look something like this.
>>+---+---------------+------+
>>|ID | PHONENUMBER | FLAGS|
>>+---+---------------+------+
>>|001| 612341234| FALSE|
>>+---+---------------+------+
>>|002| 612341235| TRUE |
>>+---+---------------+------+
>>|...| ...| FALSE|
>>+---+---------------+------+
>>|00N| 612341238| FALSE|
>>+---+---------------+------+
>>An object representation in PHP might be the object PhoneNumber. The
>>object PhoneNumber would be extended from a general class called
>>Entity. The class Entity allows for easy creation of new database
>>objects. Say I would like an EmailAddress object, I would simply
>>create the MySQL table, extend the Entity class with an EmailAddress
>>class and populate the SQL variables with the correct SQL statements.
>>Until this far everything is ok. But what if I would like an extended
>>version of the objects PhoneNumber or Emailaddress? Say for example
>>that I would like an object called MobilePhoneNumb er. The only
>>difference with the normal PhoneNumberClas s is that it is of the type
>>MobilePhoneNu mber (usage: A function only accepting MobilePhoneNumb er
>>Objects for sending text messages). I would like to create a MySQL
>>table that consists of mobilephonenume rid's and phonenumberid's .
>>What would be the best way, rewriting the internals of the Entity
>>class and just fetch the data from MySQL right away or writing new
>>internals for the MobilePhoneNumb ers class which create a
>>phoneNumberOb ject if it does not exist?
>>Hope that my problem and questions are clear, if not please ask for
>>more info.
>>Thanks in advance
>>Marijn
>Hi Marijn
>your problem is clear and well-known, particularly PoEAA book discusses
>diverse approaches in detail (see e.g. "Single Table Inheritance" and
>other ORM patterns).
>--
>gosha bine
>extended php parser ~http://code.google.com/p/pihipi
>blok ~http://www.tagarga.com/blok
Thanks for your respons Gosha. Do you have the isbn # of the book? Is
there other (online) literature you would advise me to read? What is
your experience with this problem. What I'm concerned with most is
that if I would want to extend a class which has rewritten innerworks
I have to rewrite them again and again every time I extend. This would
probably result in more coding than less, hence a bit past it's goal.
While rewriting the innerworks every time might be a more optimal
concerning speed...
Thanks,
Marijn
The official PoEAA site ishttp://www.martinfowle r.com/books.html#eaa
There's also small patterns catalog on the site.
In my personal experience, I tend to use Table Gateways rather than
diverse ORM flavors because it's much simpler and uses aggregation
instead of inheritance.
--
gosha bine
extended php parser ~http://code.google.com/p/pihipi
blok ~http://www.tagarga.com/blok

Already ordered the book on Amazon. I was wondering if anyone could
give me a small introduction to this problem in the post. Would like
to start thinking about the problem now instead of 48 hours when the
book arrives.

Thanks Marijn
I implemented it with a DBObject superclass that defines inserts,
updates, and deletes, and defines an array of fields as a property.

The fields are each of type DBField, a utility class I used to define
MySQL fields.

Now, whenever I want to create a class based on a MySQL table, I
simply extend the DBObject class and initialize the fields array. I
abstracted a function in the superclass that defines the need for an
initFields(); function in the subclass, which fills in the fields
array.

My database-enabled classes dropped from around 400 lines to around
40, and I can literally write an application in half the time it used
to take using that framework. I haven't decided whether or not to
distribute the framework itself, mostly because I'm still putting it
through it's paces on a new app before I'll bless it.

The superclass builds its own queries based on the fields array, and
knows to exclude the id field from inserts (he assumes an
auto_increment in the key). With the DBField class, you can specify
whether or not to exclude it from inserts/updates as well as specify
static values for a specific query type.

That's the basic methodology I used, and damned if I even know whether
it fits into a pattern or not, just how I decided to implement my
MySQL DB framework.

~A!

Jun 5 '07 #6

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

Similar topics

2
1898
by: Christian Decker | last post by:
I wanted to know if is a good idea to create classes that mirror exactly the structure of a database. This might sound a bit fuzzy but I'l explain myself with an example: CREATE TABLE `pl_cities` ( `city_id` int(10) NOT NULL auto_increment, `city_name` varchar(16) NOT NULL default '', `city_coordinates` varchar(10) NOT NULL default '',...
2
4185
by: Ricardo Batista | last post by:
I need that someone help me. I need to program the dijkstra algorithm by object oriented. I'll send my code. #!/usr/bin/env python # -*- encoding: latin -*- NIL =
34
3158
by: Pmb | last post by:
Hi. I'm new to this group. I'm refreshing/learning C++ and am starting to learn Object Oriented Programming (OOP). In discussing this with people I came up short as to what the benefits of OOP are. For example: As I understand it, OOP has its main benefit in software reuse. Thus one develops a software library of classes and this cuts down the...
2
1658
by: Krimgelas | last post by:
Hello, I have been programming in PHP for a while, but always the classic function oriented approach. I would like to learn how to program in PHP with an object oriented approach, but so far have found little resources. I would prefer to find a paperback book that explains this approach in a clear way, without it being too simple or too...
8
1683
by: Brian | last post by:
Hi, I come from a C background. I am trying to receive messages over a serial port. The message data structure format is along the lines of int message_id; int message_length; int data; There will be different messages received with different IDs and variable
1
1559
by: indiarocks | last post by:
I have been developing a application in Python which instantiates objects on the fly. I want to adopt a Object Oriented approach in what I am developing. So far what I have been doing is Script 1 does all the argument passing and creates a Hashed data structure which is global. It in turn instantiates script2 on the fly which is passed as...
4
1620
by: Andrew Taylor | last post by:
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...
46
2977
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented language. When I read things like "... C++ is an evolution of C ..." or "... C is a subset of C++ ..." I tend to believe that I will have to learn...
2
1531
by: dotnetnovice | last post by:
Hi...I am new to object oriented programming and facing a problem with classes and inheritance. My question is how to call members of one class in to another class. Here is class 1 class Class1 { public string Id; public string Name; public Class2 Add;
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7450
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
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...
0
6043
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...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3500
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...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.