开发者

custom php function creation and install

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开发者_JAVA技巧 input string will output the current location of the file via echo etc


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜