S
S
Stepan Zubashev2012-04-12 07:31:55
PHP
Stepan Zubashev, 2012-04-12 07:31:55

Scope of links?

Good day. On the Internet, I managed to get into a holivar about links in PHP. During the holivar, the following arguments were heard:
1. Links from PHP are about to be cut out, in version 5.0+ they were left as deprecated (I did not find any evidence for this, except for “cutting out” someFunction( &link ) )
2. Using function someFunction( ¶m ) {} is against the "function concept" because the only correct way to use a function is $some = someFunction( $some ). Any other code is “strange” and should not be written like that.
3. Instead of foreach( $array as &value ){} you should use either $array = array_map( 'func', $array ) or foreach( $array as $key => &value ){ $array[ $key ] = }.
4. Literally, “php 5 set the correct behavior. Primitives exactly need to be passed as copies (that is, so that it is impossible to influence the primitive specified in the parameter).
Such radical views on such convenient and familiar mechanisms seemed inadequate to me. I believe that every instrument has its place. And that if the use of links allows you to simplify the code, make it “more readable”, and also increase its performance, where it is critical, then there is no reason to refuse them.
I'm not saying that all functions should take reference arguments, or that code like $array = some( $array ) is bad, I'm saying that "everything has its place". But I got the impression that such a position is no longer “fashionable”, and using references even in foreach () is already unmanner.
Here is a small one riveted on the kneecode to compare performance under spherical vacuum conditions. The crux of the matter is: who is right? Is the "concept of applying a function" so strong, and references so difficult to understand, that their application is inadequate?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Beresnev, 2012-04-12
@faiwer

Saw this discussion.
1. links will not be cut out, do not go to a fortune teller ( except what you said ). Moreover, a lot of built-in functions take references as arguments.
2. This approach saves memory and time. Take at least the functions of the series sort, ksrot ... Constantly copying a large array completely would be very expensive, especially since arrays in PHP take up a lot of space.
It is also useful to use references if you want a method to return multiple values. For example, preg_match returns true/false and you can also get an array of matches. Some come up with their perversions. Imagine that preg_match can be rewritten, then the following construction could be obtained: list($is_match, $matches) = preg_match(**), which will significantly complicate the understanding of the function interface.
3. A matter of taste. But it seems to me that using a link here will give a small speed boost.
4. Well… we all use links because we use native PHP functions. And in my code I use links (as it turned out in three places: two methods, one foreach) and so far no animals have been harmed while using the code.
As for fashion, yes, programming has its own fashion. The same singletons were once popular, and now they are in disgrace. But in the case of a singleton, there are objective reasons. And here I see no objective reasons not to use links.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question