You are here: Re: Object Oriented Approach to PHP programing and MySQL database « PHP Programming Language « IT news, forums, messages
Re: Object Oriented Approach to PHP programing and MySQL database

Posted by mypetprogrammer on 06/05/07 08:54

On Jun 4, 11:06 am, Marijn <marijn.huizendv...@gmail.com> wrote:
> On Jun 4, 2:39 pm, gosha bine <stereof...@gmail.com> wrote:
>
>
>
> > On 04.06.2007 13:59 Marijn wrote:
>
> > > On Jun 4, 10:05 am, gosha bine <stereof...@gmail.com> wrote:
> > >> 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 MobilePhoneNumber. The only
> > >>> difference with the normal PhoneNumberClass is that it is of the type
> > >>> MobilePhoneNumber (usage: A function only accepting MobilePhoneNumber
> > >>> Objects for sending text messages). I would like to create a MySQL
> > >>> table that consists of mobilephonenumerid'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 MobilePhoneNumbers class which create a
> > >>> phoneNumberObject 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.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
>
> 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!

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация