Forum Moderators: coopster

Message Too Old, No Replies

calling a php function located in an include file

         

toddyboy

12:13 am on Jan 9, 2007 (gmt 0)

10+ Year Member



i am the web administrator for a large site that uses php to include code to certain parts of pages to include specific css files, depending on it's url, for example.

the way this was set up is very ugly. unfortunately, i have inherited it and don't have the time to go through and clean it up.

my problem is that i want to include another piece of code to all pages on the server, without having to go through the hassle of what i posted above. so i put a nice little include in one of these already included files to include my new piece of code.

if i execute the function in the new include page from the file it is included in, it works fine. problem lies when i call it from the actual page, it won't work, throwing up a php fatal error: call to undefined function.

i know the new file is being included as i put an echo statement in there to echo 'blah' and sure enough, blah appears on the screen.

is it because it is nested too deep? i'm going nuts trying to figure this out.

eelixduppy

12:18 am on Jan 9, 2007 (gmt 0)



Welcome to WebmasterWorld, toddyboy!

Your best bet may be to use auto_prepend_file [us2.php.net] as it will make everything much easier :)

Good luck!

Stuperfied

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

10+ Year Member



Please remember that include files are parsed before they are included. If this is a problem, simply give the file an extension other than .php so that the server will not recognise it. Also note that since a file with .php as one of multiple extensions such as .php.txt contains the extension .php, it is still recognised by the server. I personally use .inc as a substitute for .php with my include files.

As a side note, what I like to do is put all my include files in a /lib directory and include them all into a .h file first and then include that .h file in the host document. It makes it all a lot easier, maybe you might be able to do something like that.

eg:
/example/lib
example1.inc
functions...

example2.inc
classes..

/example
example.h
@include("lib/example1.inc");
@include("lib/example2.inc");

example.php
@include("example.h");

Works for me, does anyone else have a suggestion for a best practice method of handling include files? I would be very interested to hear how other people have solved the problems associated with them.

supermanjnk

3:27 am on Jan 9, 2007 (gmt 0)

10+ Year Member



My typical directory structure

./includes
./includes/config.php
./includes/functionsorclass.php
./includes/functionsorclass2.php

config.php
--------------------
Variables that can be changed
-----------------------
Sql information
-------------------------
include files
------------------
include('functionsorclass.php');
include('functionsorclass2.php');

index.php
--------------------
include ('includes/config.php');

eelixduppy

3:32 am on Jan 9, 2007 (gmt 0)



Stuperfied, you have some error in your design. ;)

Firstly, included files are not parsed:


When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

[us2.php.net...]

Secondly, ending an included php file with just .inc is a very bad thing to do. If someone accesses this file directly they will be able to view the php code itself as regular text, unless the server is specifically set to parse this extension, but this is not default. You are, however, correct in saying that filename.php.inc will be parsed correctly. :)

Stuperfied

6:04 am on Jan 10, 2007 (gmt 0)

10+ Year Member



Thats very strange because some things fail when its .php but not when its .inc. I dont know how to explain that.

eelixduppy

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



hmmmm....I'm not sure how to explain that behavior. Sounds fishy to me ;)