自定义 php 函数的创建和安装

发布于 2024-08-27 21:52:33 字数 269 浏览 9 评论 0原文

我想知道如何创建一个可以安装在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆躺在深渊里 2024-09-03 21:52:33

嗯,这里有几个选择。其中之一是通过编写扩展来实际扩展语言。您必须修改 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 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.

蛮可爱 2024-09-03 21:52:33

正如其他人所说,可能有一种更简单、更好的方法来完成大多数事情。但如果您想编写扩展,请尝试以下链接:

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

狼性发作 2024-09-03 21:52:33

因此,您想要扩展 PHP 的核心语言来创建一个名为 location() 的函数,该函数用 C 语言编写,这可以在 PHP 中通过以下方式完成:

echo __FILE__;

对。这样做很有趣。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文