PHP 架构,以及引用传递与值传递 [英] PHP architecture, and pass-by-reference vs pass-by-value

查看:52
本文介绍了PHP 架构,以及引用传递与值传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向 PHP 架构师寻求建议!

Seeking suggestions from PHP architects!

我对 PHP 不是很熟悉,但已经接管了用该语言编写的大型分析包的维护工作.该架构旨在将报告的数据读入大型键/值数组,这些数组通过各种解析模块来提取每个模块已知的报告参数.已知参数从主阵列中删除,任何模块无法识别的任何剩余部分都被转储到一种显示未知"数据点的综合报告中.

I'm not terribly familiar with PHP but have taken over maintenance of a large analytics package written in the language. The architecture is designed to read reported data into large key/value arrays, which are passed through various parsing modules to extract those report parameters known to each of those modules. Known parameters are removed from the master array, and any leftovers which were not recognized by any of the modules, are dumped into a kind of catch-all report showing the "unknown" data points.

有几种不同的方法用于调用这些解析器模块,我想知道哪些被认为是正确的"PHP 结构.有些是使用传引用,有些是传值,有些是函数,有些是对象.它们都以某种方式修改了输入参数.

There are a few different methods being used to call these parser modules, and I would like to know which if any are considered to be "proper" PHP structure. Some are using pass-by-reference, others pass-by-value, some are functions, some are objects. All of them modify the input parameter in some way.

一个超级简化的例子如下:

A super-simplified example follows:

#!/usr/bin/php
<?php

$values = Array("a"=>1, "b"=>2, "c"=>3, "d"=>4 );


class ParserA {
    private $a = null;
    public function __construct(&$myvalues) {
        $this->a = $myvalues["a"];
        unset($myvalues["a"]);
    }
    public function toString() { return $this->a; }
}

// pass-by-value
function parse_b($myvalues) {
    $b = $myvalues["b"];
    unset($myvalues["b"]);
    return Array($b, $myvalues);
}

// pass-by-reference
function parse_c(&$myvalues) {
    echo "c=".$myvalues["c"]."\n";
    unset($myvalues["c"]);
}

// Show beginning state
print_r($values);

// will echo "1" and remove "a" from $values
$a = new ParserA($values);
echo "a=".$a->toString()."\n";
print_r($values);

// w ill echo "2" and remove "b" from $values
list($b, $values) = parse_b($values);
echo "b=".$b."\n";
print_r($values);

// will echo "3" and remove "c" from $values
parse_c($values);
print_r($values);

?>

输出将是:

Array
(
    [a] => 1
    [b] => 2
    [c] => 3
    [d] => 4
)
a=1
Array
(
    [b] => 2
    [c] => 3
    [d] => 4
)
b=2
Array
(
    [c] => 3
    [d] => 4
)
c=3
Array
(
    [d] => 4
)

使用这么多不同的调用方法我真的很不舒服,其中一些对使用&pointer"风格的函数的调用函数参数有隐藏的影响,一些需要主体编写它们的输出,还有一些独立编写他们的输出.

I'm really uncomfortable having so many different call methods in use, some of which have hidden effects on the call function parameters using "&pointer"-style functions, some requiring the main body to write their output, and some writing their output independently.

我更愿意选择一种方法并坚持下去.为此,我还想知道哪个最有效;我对 PHP 文档的阅读表明,由于它使用写时复制,因此使用指向与直接传递对象的指针和重新读取返回值之间应该没有太大的性能差异.我也更喜欢使用面向对象的结构,但对构造函数中对输入参数所做的隐藏更改感到不舒服.

I would prefer to choose a single methodology and stick with it. In order to do so, I would also like to know which is most efficient; my reading of the PHP documentation indicates that since it uses copy-on-write, there shouldn't be much performance difference between using pointers to vs passing the object directly and re-reading a return value. I would also prefer to use the object-oriented structure, but am uncomfortable with the hidden changes being made to the input parameter on the constructor.

在 ParserA()、parse_b() 和 parse_c() 三种调用方法中,哪一种是最合适的风格?

Of the three calling methods, ParserA(), parse_b(), and parse_c(), which if any is the most appropriate style?

推荐答案

我并不是真正的 PHP 专家,但根据我的经验,传递值更好.这样代码不会有副作用,这意味着它会更容易理解和维护,并且可以在上面做各种疯狂的事情,比如将它用作 map 函数的回调.所以我完全赞成 parse_b 做事的方式.

I'm not really an expert in PHP but from my experience passing by value is better. This way code won't have side effects and that mean it will be easier to understand and maintain and do all sorts of crazy things on it, like using it as callback for map function. So I'm all for parse_b way of doing things.

这篇关于PHP 架构,以及引用传递与值传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆