You are here: Re: DB classes in PHP 4: include file hell « PHP Programming Language « IT news, forums, messages
Re: DB classes in PHP 4: include file hell

Posted by Toby A Inkster on 03/06/07 10:09

ooba gooba wrote:

> - Because I'm using MySQL 4.1 and MyISAM tables (InnoDB is too darn
> big), I have to manage all the relationships myself.

Upgrade your hosting. Use MySQL 5, or switch to PostgreSQL, Oracle or
Microsoft SQL Server. Any of those can properly handle foreign keys and
cascaded deletes and handy things like that.

> - If I need to delete an entity that has a lot of relationships, I have
> to delete the records from all the related tables.

Upgrade your hosting. A decent database can do cascaded deletes. e.g.:

DELETE FROM users WHERE userid='frank';

can automatically remove, say, all entries in the "articles" table where
Frank was the author.

> - 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.

Don't include() the files until you know you're going to use them.

e.g. Don't do this:

include "foo-function.php";
if ($something)
{
foo($blah);
}

Do this:

if ($something)
{
include_once "foo-function.php";
foo($blah);
}

Then your file only gets included if it's needed. Contrary to popular
wisdom, include() and require() statements do not need to be at the top of
your file, and there is usually no advantage in putting them there.

> - 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.

Upgrade your hosting. PHP 5 has class autoloading.

http://www.php.net/autoload

So if you do something like:

<?php $obj = new MyClass(); ?>

and MyClass has not yet been defined, then it will automatically call a
function __autoload() passing 'MyClass' as the parameter. You can define
the __autoload() function yourself, so that it does something useful like:

<?php
function __autoload ($c)
{ require_once $c.'.class.php'; }
?>

or whatever your naming convention is.

> 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?

The better solution is to switch to a decent host that gives you better
tools to work with, including an ACID database that supports foreign keys
and cascading, and a recent version of PHP. The amount of effort saved by
switching to a more functional programming environment should more than
outweigh the effort involved in switching web hosts.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

 

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

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