简体   繁体   中英

Is it possible for a variable to reference itself

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM