Reply to Re: DB classes in PHP 4: include file hell

Your name:

Reply:


Posted by Jerry Stuckle on 03/06/07 11:36

ooba gooba wrote:
> After doing some work in both Java and ASP.NET, I came back to add some
> major enhancements to a PHP 4 application I built several years ago. A
> lot of the database access is currently sprinkled throughout the
> application.
>
> In the Java and .NET worlds, I would do data access code by creating
> classes that would handle all of the data access for each of the
> application data entities. This worked great and kept all the data
> access in one clearly designated set of classes.
>
> I thought I would apply the same concept to PHP. However, it isn't
> working well, and here's why:
>
> - Because I'm using MySQL 4.1 and MyISAM tables (InnoDB is too darn
> big), I have to manage all the relationships myself.
>

You get some and you lose some. You'll need to use InnoDB tables to get
the MySQL to handle the relationships. But I've found it really isn't
that bad.

> - If I need to delete an entity that has a lot of relationships, I have
> to delete the records from all the related tables. I can do this by
> either (a) making direct calls to MySQL to delete records from all the
> related tables, or (b) if this was Java or .NET, I would simply make
> calls to the related data objects. Since some of the related tables also
> had related operations to perform, I started revising my PHP code to use
> the latter method. So my code started to look like this:
>
> class HcsDBO
> {
> // Delete an HCS and all its related records
> function Delete($hcs)
> {
> if (!empty($hcs))
> {
> HospitalsDBO::DeleteHCSHospitals($hcs);
> UsersDBO::DeleteHCSUsers($hcs);
> BillToDBO::Delete($hcs);
> DuesDBO::Delete($hcs);
>
> // Now delete the HCS record
> global $db;
> $sql = "DELETE FROM HCS WHERE EntityID=$hcs";
> $db->Query($sql);
> }
> }
> }
>
> - In order to call these other data object methods, I need to include
> their files. This means that whenever I need to perform any kind of HCS
> operation, whether or not related tables are involved, I need to include
> all the data access code for all the related tables. That's going to be
> a LOT of code.
>

Yep, that's true. But the interpreter is pretty good.

> - Compiled (or pseudo-compiled) languages such as Java and .NET can pull
> in called objects as needed. PHP doesn't. All that code is read and
> parsed for every page that requires any of it.
>
> I can't see that this approach is very efficient for PHP, but I don't
> want to continue the "data access code is sprinkled everywhere"
> approach. Unfortunately, the web host does not support PHP 5.
>
> Is there a better solution, or should I revert to the "sql code
> sprinkled everywhere" approach?

Either use require_once() to get them when you need them (which is why I
do - with no noticeable effect on performance) or go to InnoDB tables.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original 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

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