Page 1 of 1

include hell

Posted: Sat Mar 01, 2008 7:46 am
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...

Re: include hell

Posted: Sat Mar 01, 2008 8:48 am
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?

Re: include hell

Posted: Sat Mar 01, 2008 9:54 am
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:

Re: include hell

Posted: Sat Mar 01, 2008 9:58 am
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....

Re: include hell

Posted: Sat Mar 01, 2008 12:55 pm
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 ();

Re: include hell

Posted: Sat Mar 01, 2008 3:05 pm
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

Re: include hell

Posted: Sat Mar 01, 2008 7:52 pm
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() )??

Re: include hell

Posted: Sat Mar 01, 2008 8:19 pm
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!

Re: include hell

Posted: Sat Mar 01, 2008 8:42 pm
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

Re: include hell

Posted: Sat Mar 01, 2008 8:58 pm
by skooter
:banghead: :banghead: :banghead:

Re: include hell

Posted: Sat Mar 01, 2008 9:14 pm
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.

Re: include hell

Posted: Sat Mar 01, 2008 9:34 pm
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....