include hell

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

include hell

Post by skooter »

ok, so I'm deving a site for a friend. It's all modular, which seems to be the problem. I've got the following classes:

Table.php
DB.php
AdminUpdateDB.php
DBConfig.php
AdminConfig.php

where each file corrosponds to a class. The problem is that several of the classes require others to be included (eg DB needs DBConfig and Table, AdminUpdateDB needs AdminConfig and DB). They all work fine individually (as classes, tested as individual html pages, no errors occurring) yet as I started to add it all together nothing parsed. Or a page would partially parse, hit an include and stop. So I figure that there are include/require clashes (and i've tried all possible combinations of require/include/require_once) and decided to create a file with all related includes, to be added as a header to any file that needs one (or more of the classes), which is:

Code: Select all

 
<?php
/*
 * Created on 01/03/2008
 *
 * Include file to include all classes where necessary
 * should be included on any output page that requires classes....
 */
 
require_once("./Admin/Table.php");
require_once("./Admin/DB.php");
require_once("./Admin/AdminUpdateDB.php");
require_once("./Admin/DBConfig.php");
require_once("./Admin/AdminConfig.php");
?>
 
Now nothing works. I've tried the requires in the reverse order (so that each class is preceeded by its necessary includes), tried it with includes etc. This parses fine as an individual class, yet when added at the top of a page that needs the includes it breaks.

Sidenote: efficiency for this section of the site is irrelevant as its an admin section only. The public domain does not require either of the Admin pages...
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: include hell

Post by Sekka »

This is a wierd one.

Where are the PHP files in relation to the PHP file with the requires are in? As the require_once() path need correspond to where that file is.

I've never heard of this problem before. The only thing that would stop in include is if the file can't be found or it parses incorrectly.

Have you got display_errors on for debugging?
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: include hell

Post by skooter »

okies. I have: E_STRICT and E_ALL on, with display_errors On and nothing appears.

The file addresses are ok. As i wrote, when I created a html page from Classes.php (with all the includes) it ran fine, but when I include that in another page it dies...

Can includes be chained in this way?? :banghead:
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: include hell

Post by skooter »

I think fundamentally what I'm wondering is can I do this?? If I include all the files in a superfile, with the included files have access to the other classes in the superfile. Otherwise the object-instantiation in the classes will fail...

But then, if they DONT...why was the includes clashing in the first place....
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: include hell

Post by Sekka »

If display errors is on and error reporting is E_ALL and you still get a blank screen, the includes are working fine.

This may be a stupid question, but I need to ask it. When you include the files, aka the classes, are you instantiating them? e.g. $db = new DBClass ();
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: include hell

Post by Christopher »

skooter wrote:It's all modular, which seems to be the problem.
Your problem is you probably do not have clean dependencies, so it is actually not "all modular".

You need to clean your dependencies up. The dependencies you give are:

Table -> ?
DBConfig -> ?
AdminConfig -> ?
DB -> DBConfig
DB -> Table
AdminUpdateDB -> AdminConfig
AdminUpdateDB -> DB
(#10850)
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: include hell

Post by skooter »

All classes are being instantiated.

Its the dependancies that I'm wondering about. Does each class *have* to have its required includes (bad pun!) in it?? Or can they be inherited from eg. a superinclude file or the actual html page itself? Do include clashes work as I suspected. I found a website that mentions they do (eg including the same page twice on another page will cause a die() )??
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: include hell

Post by Sekka »

If you always use require_once() then you will never include a file twice, no matter how many times you an include it and on what page.

As for the dependencies, yes, you must include every dependency in each file. This will solve problems!
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: include hell

Post by skooter »

btw: thanks for your help and quick replies....

I've been using require_once, but I might go back to that and see what happens
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: include hell

Post by skooter »

:banghead: :banghead: :banghead:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: include hell

Post by Christopher »

skooter wrote:Its the dependancies that I'm wondering about. Does each class *have* to have its required includes (bad pun!) in it?? Or can they be inherited from eg. a superinclude file or the actual html page itself? Do include clashes work as I suspected. I found a website that mentions they do (eg including the same page twice on another page will cause a die() )??
I am assuming that you have one class per file, though you haven't said so. There are several ways you can include the files. The only requirement is that the file containing the class definition has been included before the class is used. The exception to this is if you use __autoload() or SPL autoload. So you can include the file at any point, all at once or just before the class is defined. If you use autoload you don't need to include the files containing the classes -- if your autoload function is setup properly.
(#10850)
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: include hell

Post by skooter »

Mostly one class per file. DB.php has the DB class and three subclasses.

I've eradicated the Table file (since it only had one function that was only called by DB), and cleaned up a few other clashes, but the prevailing clash seems to be the use of the Config files. These only contain constants and getters, so I'm starting to think that I'll just cut'n'paste the constant values into the appropriate classes and see what happens....its bad SE, I know but atm getting this running is more important to me....
Post Reply