我们可以在 php 中实现 php 提取功能而不使用 zend api

发布于 2024-08-21 11:16:32 字数 64 浏览 3 评论 0原文

php的Extract函数将变量添加到调用函数的本地范围内。我们如何编写自己的 php 函数摘录来实现相同的功能?

Extract function of php adds variables to the local scope of the calling function. How can we write our own php function extract which does the same?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

熊抱啵儿 2024-08-28 11:16:32

我认为你不能在纯 PHP 中做到这一点:函数无法访问其调用者的变量。

这看起来像是您需要编写 C 扩展来开发的东西——您将在那里对 PHP 变量的内部有更多的控制。

(我真的不明白你为什么要这样做:为什么不直接使用现有的 提取函数?)

I don't think you can do that in pure-PHP : a function doesn't have access to the variables of its caller.

This looks like the sort of thing you'll need to write a C extension to develop -- you'll have more control over the internals of PHP variables there.

(And I don't really see why you're trying to do that : why not just use the existing extract function ? )

喜爱纠缠 2024-08-28 11:16:32

PHPJS 是一个 javascript 库,它包含了 php 的功能,这里是它的 extract 函数您可以从中得到一些想法。话虽如此,我想知道为什么当 php 已经提供了一个函数时你还要寻找自己的函数。

PHPJS is a javascript library which contains the functions as those of php, here is its extract function you can get an idea from. Having said that, i wonder why are you looking for your own function when php already provides one.

錯遇了你 2024-08-28 11:16:32

好吧,如果只是为了好玩,这是可能的:

<?php
$data=array('a' => 1, 'b' => 2);

foreach ($data as $name => $value) {
    ${$name} = $value;
}

echo $a;
?>

但是有些问题 - 例如与 $data、$name 和 $value 冲突,并且你不能将其包装在函数中。但有可能。

Ok, if it is just for the fun of it it is possible:

<?php
$data=array('a' => 1, 'b' => 2);

foreach ($data as $name => $value) {
    ${$name} = $value;
}

echo $a;
?>

But some problems - like conflicts with $data, $name and $value and you can't wrap it in a function. But possible.

瀞厅☆埖开 2024-08-28 11:16:32

是的,有可能,

我这样做的原因是因为我需要从提取函数中获得更多内容,它也应该修剪这些值

function extractArray($ar) {
        if(count($ar)>=1) {
            foreach($ar as $k=>$v) {
                $GLOBALS[$k] = (is_array($v)?$v:trim($v));
            }
        }
}

我只是传递我的 $_GET、$_POST 和其他像这样的数组
例如
$_GET['用户名'] = 'justnajm';
提取数组($_GET);

我可以只是回显,它会打印“justnajm”

Yes it is possible,

the reason why i did this because i need something more from extract function that it should trim the values too

function extractArray($ar) {
        if(count($ar)>=1) {
            foreach($ar as $k=>$v) {
                $GLOBALS[$k] = (is_array($v)?$v:trim($v));
            }
        }
}

I just pas my $_GET, $_POST, and else arrays like this
for e.g.
$_GET['username'] = ' justnajm ';
extractArray($_GET);

and i can just echo and it will print 'justnajm'

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