views:

62

answers:

3

I would like to know how to create a php function that can be installed in php just like the already built in functions like :

rename
copy 

The main point I would like to achieve is a simple php function that can be called from ANY php page on the whole host without needing to have a php function within the php page / needing an include.

so simply I would like to create a function that will work like this :

location();

That without a given input string will output the current location of the file via echo etc

A: 

So you want to extend PHP's core language to create a function called location(), written in C, which could be done in PHP by:

echo __FILE__;

Right. Have fun doing that.

Coronatus
Sarcasm goes in the comments section.
Andy E
This isnt exactly what I meant, the Location() module was an example of what I was intending on accomplishing I realise that echo __FILE__; will output the file's location completely and thankyou. The main question I was attempting to ask was how would I go about installing a built in function into php?
Snake
Sarc is my natural dialect.
Coronatus
Do you have any Idea's in how I can accomplish installing my own custom php function like this?
Snake
This is a good tutorial on writing extensions: http://devzone.zend.com/article/1021
Coronatus
+6  A: 

Well, there are a couple of options here. One of them is to actually extend the language by writing an extension. You'd have to muck around with the PHP source code, write it in C, and deal with the Zend Engine internally. You probably wouldn't be able to use this on a shared host and it would be quite time consuming and probably not worth it.

What I would do is put all of your functions into a separate PHP file, say helper_functions.php. Now, go into your php.ini and add the directive: auto_prepend_file = helper_functions.php. This file should be in one of the directories specified in your include_path (that's a php.ini directive too).

What this does is basically automatically put include 'helper_functions.php'; on every script. Each and every request will have these functions included, and you can use them globally.

Read more about auto_append_file.

ryeguy
Thankyou for your Great response to my question I much appreciate this, Just to verify what your proposing : Instead of mucking into the php zend and writing it in C# which would both be time consuming + possibly not compatible for mass instilation across multiple machines you've recommended editing the PHP.ini and placing auto_prepend_file This will include a given php functions file on every page load , if a function is not called , no response is given just as if I Included the file within the php source code.This Include_path could be any given directory correct?
Snake
If you wrote an extension, it'd have to be in C not C# (that wouldn't be too bad). You're correct, no response will be given if you do not call those functions as long as all you have is function declarations. It's still a regular php file though, so if you had `echo 'hello!';` at the top of your script, you'd still see that `echo`'d every request. Yes, `include_path` can be any directory, read more about it here: http://www.php.net/manual/en/ini.core.php#ini.include-path
ryeguy
So Could I potentialy include a php file outside of the www ?directory and in a user area that the php + webserver has permision to browse too Thankyou very much :)
Snake
Yes you can, as long as you specify it in your `include_path`. Also, if you like this answer, please upvote it and accept it (the up arrow and checkmark to the left) :).
ryeguy
sure , thankyou very much could you possibly give me a small example for referance? ( to another folder / user area ) - because the referances im pulling from are explaining about current directory includes ( Im suspecting that it works like a normal include ) anyway thankyou very much + 1
Snake
Exactly which folder are you putting it in? What is your `include_path` set to? Did you remember to restart your web server after you changed `php.ini`?
ryeguy
anyway I think the only question I should ask at the momment before I close this question is: You can place small minor php.ini files in directory's to place certain customisations to that folder , will this be possible if I only wanted the php files within c:\webserver\www\support\ by creating a php.ini file with the include setting within there?
Snake
I'm pretty sure that will work. You should try it yourself though, I don't have much experience using per-directory configuration files. I don't see why it wouldn't work, though.
ryeguy
+1  A: 

As others have said, there's probably an easier, better way to do most things. But if you want to write an extension, try these links:

http://docstore.mik.ua/orelly/webprog/php/ch14_01.htm

http://www.tuxradar.com/practicalphp/2/3/0

Scott Saunders
thankyou very much for your help , this has helped me allot also and was exactly what I needed initialy thankyou, This is practicly a bullseye on the target thankyou very much its helped my understanding tremendously
Snake