473,654 Members | 3,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DB classes in PHP 4: include file hell

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.

- 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::D eleteHCSHospita ls($hcs);
UsersDBO::Delet eHCSUsers($hcs) ;
BillToDBO::Dele te($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.

- 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?
Mar 6 '07 #1
3 1768
- Because I'm using MySQL 4.1 and MyISAM tables (InnoDB is too darn
big), I have to manage all the relationships myself.
I run PHP and MySQL myself on an ancient home computer with win98 and
far too little disk space. It runs fine. With InnoDB, that is.

<snip>
- 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.
Why not? PHP is an interpreter language. Loading things only when you
need them is quite efficient. If you use the require_once statement, you
can make sure that you don't define things twice with includes.

Be careful though, that PHP first tries to resolve relative paths
relative to the "browsed" file, not to the currently included file. Best
to use absolute paths only. So instead of:
require_once('. ./database.php');
you could write:
require_once(di rname(__FILE__) . '/../database.php');

There is no difference when you point your browser to that file, but
there is a difference when you include a file with the above contents
from another directory.

Best regards
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Mar 6 '07 #2
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!
Mar 6 '07 #3
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::D eleteHCSHospita ls($hcs);
UsersDBO::Delet eHCSUsers($hcs) ;
BillToDBO::Dele te($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.
js*******@attgl obal.net
=============== ===
Mar 6 '07 #4

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

Similar topics

12
7408
by: bissatch | last post by:
Hi, Generally if I re-use code, I use a function. If I need to use these functions over a number of pages I write the function to an include file where all pages have access. So when should I ever use PHP classes instead. I have learned how to put together PHP classes but never seen a reason to use them where I can simply use a function.
5
1698
by: Kaila | last post by:
I'm using metroworks codewarrior and can't solve a redeclariotin of type problem I have several classes and each of them need to have a custom type called enum X now what I was thinking is to put the type in a seperate file and simply attach it as a librabry to my main as follows: In main.cpp
9
6632
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to use the common example, I have a situation where I am implementing many types of Shapes. My current way of thinking is, well, since they are all the same type, let's just put them all in the same file. The include file would be "shapes.h" and it...
28
3871
by: Ramesh | last post by:
Hi, I am currently maintaining a legacy code with a very very large code base. I am facing problems with C/C++ files having a lot of un-necessary #includes. On an average every C/C++ file has around 150+ .h files included. I find 75% of the files unnecessary and could be removed. Considering the fact that I have a huge code base, I can't manually fix it. Are there any tools that would report un wanted .h files?
10
2288
by: Noozer | last post by:
I'm writing an ASP application and have a noob question... I have a class that access an MS SQL database. I have another class also accesses an MS SQL database and this second class uses objects from the first class. I have a third class using the DB and objects of the second class. Each of these classes contain all the code needed to access the database and this means much duplicated code. What I'd like to know is if there is a way...
13
1724
by: Simon Dean | last post by:
Hi, I have a couple of questions. If you don't mind. Sorry, I do get a bit wordy at times. First one just throws some thoughts around hoping for answers :-) 1) Anyone got a theory on the usage of PHP Classes rather than an actual technical guide? I've seen loads of things that show how to put together a class, but without actually necessarily saying why you'd want to use a class over say a separate file of functions or explaining:
5
2154
by: andrewmorrey | last post by:
Hello, I've got a VC++ project containing multiple classes and a main function. In one of the class functions, it reads from a text file and places the data into a vector; // std::vector<std::vector<std::string applications (50, std::vector<std::string>(12)); applications = "Test1"; applications = "Test2";
1
2784
by: Swapnil Kale | last post by:
Hi, I'm working on a Migration project (Forte to JAVA). The forte client had a C++ dll which used to call one more FORTE dll for a complex database calculations. Now all the forte code has been migrated to JAVA except this piece of code where C++ dll calls Forte DLL.
1
1475
by: puzzlecracker | last post by:
Hello Group, Say I have classes A and B and the both need to maintain a pointer/ reference to each for various reasons, say optimization. And there are other classes in the project use these two classes, among others. What is the best design for such architecture? I saw errors by explicitly including headers of both classes. Also,
0
8375
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
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
8707
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...
1
8482
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7306
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...
0
4149
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...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
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
2
1593
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.