Implementing built in functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nickspick
Forum Newbie
Posts: 7
Joined: Fri Oct 08, 2004 6:34 am
Location: Brighton, UK

Implementing built in functions

Post 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
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

and there's auto_prepend ini directive...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

nickspick
Forum Newbie
Posts: 7
Joined: Fri Oct 08, 2004 6:34 am
Location: Brighton, UK

Post 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'?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
nickspick
Forum Newbie
Posts: 7
Joined: Fri Oct 08, 2004 6:34 am
Location: Brighton, UK

Post by nickspick »

thanks for the advice
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

Post 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
Post Reply