Forum Moderators: coopster

Message Too Old, No Replies

Where to save PHP class files

Equivalent to CLASSPATH or @INC

         

timster

2:17 am on Feb 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Forgive this novice question, but the tutorials I see all skip this part.

Where do I save a PHP class file so that PHP pages can find it without an include file?

I'm asking for the equivalent of the JAVA CLASSPATH or the Perl @INC array.

Example:
include("/some/path/SomeClass.php"); # Would like to get rid of this

$object = new SomeClass();

eelixduppy

2:30 am on Feb 19, 2008 (gmt 0)



To get rid of the include completely you can use something like the auto_prepend_file [us2.php.net] directive in your php.ini configuration file. Setting this will automatically include the class files for you.

mehh

10:10 am on Feb 19, 2008 (gmt 0)

10+ Year Member



There is also the __autoload function [uk3.php.net] avalible in php5. for example this at the top of your file will attempt to load classes from /path/to/clasees
[pre]function __autoload($class_name) {
$file='/path/to/classes/' . $class_name . '.class.php';
if(file_exists($file)){
require_once $file;
}
}[/pre]

timster

3:14 pm on Feb 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the replies.

I was researching them and I found the "include_path" directive for the php.ini file. This is what I was looking for, I think.

It will allow removing information on the directory structure of the site from the web site's document root, as a security precaution.