自定义 php 函数的创建和安装
我想知道如何创建一个可以安装在php中的php函数 就像已经内置的函数一样:
rename
copy
我想要实现的要点是一个简单的 php 函数,可以从整个主机上的任何 php 页面调用,而不需要在 php 页面内有 php 函数/需要包含。
所以我想创建一个像这样工作的函数:
location();
如果没有给定的输入字符串,将通过 echo 等输出文件的当前位置
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,这里有几个选择。其中之一是通过编写扩展来实际扩展语言。您必须修改 PHP 源代码,用 C 语言编写,并在内部处理 Zend 引擎。您可能无法在共享主机上使用它,这将非常耗时并且可能不值得。
我要做的就是将所有函数放入一个单独的 PHP 文件中,例如
helper_functions.php
。现在,进入php.ini
并添加指令:auto_prepend_file = helper_functions.php
。该文件应该位于include_path
中指定的目录之一(这也是一个php.ini
指令)。它的作用基本上是自动将
include 'helper_functions.php';
放在每个脚本上。每个请求都将包含这些功能,并且您可以在全局范围内使用它们。了解有关 auto_append_file 的更多信息。
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 yourphp.ini
and add the directive:auto_prepend_file = helper_functions.php
. This file should be in one of the directories specified in yourinclude_path
(that's aphp.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.
正如其他人所说,可能有一种更简单、更好的方法来完成大多数事情。但如果您想编写扩展,请尝试以下链接:
http://docstore .mik.ua/orelly/webprog/php/ch14_01.htm
http: //www.tuxradar.com/practicalphp/2/3/0
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
因此,您想要扩展 PHP 的核心语言来创建一个名为
location()
的函数,该函数用 C 语言编写,这可以在 PHP 中通过以下方式完成:对。这样做很有趣。
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:Right. Have fun doing that.