Page 1 of 1

Implementing built in functions

Posted: Thu Oct 14, 2004 7:11 am
by nickspick
Is there a simple way of implementing a function into PHP so that it can be called by any script even if it hasn't been included somewhere yet.

The function below uses the number of '/' in the path to determine the pages level within a site. I need this value before I can successfully include(); a file. For example, if a file was located at '/folder/folder/folder/file.php' the result would be '../../../'

Code: Select all

function get_dir() {
	$path = $_SERVER['PATH_INFO'];
	$path = explode("/",$path);
	$num_levels = count($path) - 2; 
	for($i=0;$i<$num_levels;$i++) { $dir.= "../"; }
	return $dir;
}
I could then place $dir within an include() to include a file in the root folder.

A solution is to just place the contents of the function on any page that needs the value but it would be much cleaner to just be able to call the function.

I know that this could be acheived by producing a PHP extension that contained the function but I have no knowledge of how to produce DLL's.

Cheers, Snick

Posted: Thu Oct 14, 2004 7:54 am
by phpScott
you could modify your php.ini file and look for the include_path and set up the directory there.

would be one way the other is to use absolute paths as in c:\apache2\apache\htdocs\commonfiles\

as that would work as well but of course neither suggestion is very easy to move your site from one place to another.

Posted: Thu Oct 14, 2004 8:00 am
by Weirdan
and there's auto_prepend ini directive...

Posted: Sun Oct 17, 2004 7:23 pm
by feyd

Posted: Mon Oct 18, 2004 5:26 am
by nickspick
I've looked into modifying the ini file or using set_include_path but it would make it complicated moving the site from one place to another. As for generating an ini file, I've looked at the documentation in the above links, it doesn't look like it'll be too complicated but is still over my head. Do I copy the sample code 'Example 47-1. A simple extension.' into a file and save that in PHP/extensions. and then run 'buildconf'? How do I run 'buildconf'?

Posted: Mon Oct 18, 2004 10:58 am
by feyd
It sounds like you haven't written a binary application before. I wouldn't start in creating an extension for php. It's a bit more complicated than a beginner should try.

Posted: Tue Oct 19, 2004 5:20 am
by nickspick
thanks for the advice

Posted: Tue Oct 19, 2004 5:28 am
by harsha
Best method is use PHP Nuke style of approach it eliminated the confusion of abs. path and rel. path

thatz what i feel