变量是否可以引用自身
I've never seen something like this in a language, but I'm working with a PHP array and it would quite useful, but more than any thing, I'm curious.
Is it possible for a regular variable to reference itself. For example:
$variable_array = array(1, 2);
$variable_array = array_merge(self, array(3, 4));
or
$variable_string = 'This is a string';
$variable_string = explode(' ', self);
Where self
is the variable itself, like this
is when working with objects. Now, I know someone is going to ask why not just call the variable again, and that is what I normally do in this case. However, for readability when dealing with long names, such as named indexes in arrays, this would be useful.
Does PHP, or any language at all do this or something similar?
我从未在某种语言中看到类似的东西,但我正在使用PHP数组,它会 非常有用,但比任何事情更重要,我很好奇。 p>
常规变量是否有可能引用自身。 例如: p>
$ variable_array = array(1,2);
$ variable_array = array_merge(self,array(3,4));
code> pre>
或 p>
$ variable_string ='这是一个字符串';
$ variable_string = explode('',self); \ n code> pre>
其中 self code>本身就是变量,就像这个 code>在处理对象时一样。 现在,我知道有人会问为什么不再调用变量,这就是我在这种情况下通常做的事情。 但是,为了在处理长名称(例如数组中的命名索引)时的可读性,这将是有用的。 p>
PHP或任何语言都可以执行此操作或类似的操作吗? p >
div>
So, thanks to RandomSeed reminding me of variable references, I came up with what I think is the closest solution I'm likely to get.
Take this as an example:
$array['index1']['index2']['index3']['index4'];
$array['index1']['index2']['index3']['index4'] = array_merge(
$array['index1']['index2']['index3']['index4'],
array('test' => 'answer'),
array('test2' => 'answer'));
This is unappealing in my eyes, and a bit difficult to follow, especially if there are multiple of these on a page, or in a controller action.
So this is an alternative:
$self = &$array['index1']['index2']['index3']['index4'];
$array['index1']['index2']['index3']['index4'] = array_merge(
$self,
array('test' => 'answer'),
array('test2' => 'answer'));
A Quick test on my live environment seems to verify that it works.
Is it possible for a regular variable to reference itself
No (in the way you go on to describe it), it is not. Nothing will be more clear than using the identifier (the variable name). That's exactly what it's for.
$variable_array = array_merge($variable_array, array(3, 4));
Imagine:
$a = $b = f(self);
We could come up with a rule to handle this but why bother when we can make it ultimately clear with:
$a = $b = f($a);
readability when dealing with long names, such as named indexes in arrays
You may be thinking of Visual Basic's With
PHP does not have an elegant equivalent. You could just:
$shortName = $veryLongName['with']['many']['indexes'];
The answer to your headline question is yes: $a = &$a;
. But that's not what you really wanted to ask.
In fact, you are precisely expecting the features of an object:
assign a variable the return value of a function that takes the variable as a parameter
$object->modify(); // no parameter, no assignation
Alternatively, you might be looking for a function that takes a parameter as a reference:
function foo(&$bar) {
$bar = 'buzzle';
}
$qux = 'fizz';
foo($qux);
echo $qux; // output: "buzzle"
Nothing else.
If the only thing reason you are interested in this is
"...for readability when dealing with long names, such as named indexes in arrays..."
Then, considering you are going to have to type out the long name at least once anyway, I suppose you could just create a short named reference to the long named thing and use that.
$short = &$for_example['really']['long']['names']['like']['this'];
$short = explode(' ', $short); // etc.
But this seems like it might end up creating more confusion than it would be worth.