Forum Moderators: coopster

Message Too Old, No Replies

nested includes

how to call a function several levels deep

         

lethal0r

10:57 pm on Jan 1, 2007 (gmt 0)

10+ Year Member



index.php includes includefile1.php which includes includefile2.php

how do i call a function in includefile2.php from index.php?

ive tried $includefile1.php->function(); but it does not work. can someone help me out?

also is designing a site in such a way a good idea? or should I have includes on 1 level only?

mcavic

12:01 am on Jan 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You just call function() and it'll behave as if there was only one file with everything in it. I don't see why it would be a problem, although I would try to avoid nested includes for simplicity.

IanKelley

9:52 am on Jan 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Nested includes also force more disk accesses.

lethal0r

3:45 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



sorry i think i should have mentioned each include file is an object.

currently in includefile1 i have

function __construct() {
include('includefile2.php');
}

and then in the calling function I set

global $includefile2;

then i try $includefile2->function() and just function() but im getting a Call to undefined function error.

henry0

4:03 pm on Jan 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could simply include A) and include B)
or set all functions in a class and use the class
since you know about object you should not have any problems.

It will make you code cleaner and more understandable to others or your goodself if you come back and revisit your script after a few months

lethal0r

4:29 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



thanks for the replies. my idea is to have a platform independent object that includes several platform specific objects. thats why i chose to nest it in such a manner. the idea being that all function calls for any platform can just go to the same object.

when i do

function __construct() {
include('includefile2.php');
echo is_object($includefile2);
}

this outputs a 1 and shows it is working here ok. but when i do the same thing in the calling function:

function function() {
global $includefile2;
echo is_object($includefile2);
}

it outputs nothing, not even a 0. am i missing something obvious?

mcavic

4:42 pm on Jan 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is $includefile2? Include files don't exist as objects or anything while the script is running. All the files are combined into one script in memory, then compiled and run.

lethal0r

4:55 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



it is an object. its contents are

class IncludeFile2 {

<functions here>

}

$includefile2 = new IncludeFile2();

lethal0r

7:29 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



i've just read that you apparently can't nest classes in PHP.

what does work though is if I put the include statement in the calling function instead of the object constructor. now I have:

function function() {
include('includefile2.php');
$includefile2->function();
}

and it works.