Kcko
3/1/2019 - 7:04 PM

Returning value by reference // fn

RETURNING VALUE BY REFERENCE // FN

<?php

class Fruit {
    private $color = "red";

    public function getColor() {
        return $this->color;
    }

    public function &getColorByRef() {
        return $this->color;
    }
} 



echo "\nTEST RUN 1:\n\n";
$fruit = new Fruit;
$color = $fruit->getColor();
echo "Fruit's color is $color\n"; 
$color = "green"; // does nothing, but bear with me
$color = $fruit->getColor();
echo "Fruit's color is $color\n"; 

echo "\nTEST RUN 2:\n\n";
$fruit = new Fruit;
$color = &$fruit->getColorByRef(); // also need to put & here
echo "Fruit's color is $color\n"; 
$color = "green"; // now this changes the actual property of $fruit
$x = $fruit->getColor();
echo "Fruit's color is $color\n"; 
class Demo
{
    protected $example = 'original value';

    function & example()
    {
        return $this->example;
    }
}

$demo = new Demo;

#1 - pass by value

$var1 = $demo->example();
$var1 = 'var1 value';

echo '$demo->example() => '.$demo->example().PHP_EOL;
echo PHP_EOL;

#2 - pass by reference

$var2 =& $demo->example();
$var2 = 'var2 value';

echo '$demo->example() => '.$demo->example().PHP_EOL;
echo '$var1 => '.$var1.PHP_EOL;
echo '$var2 => '.$var2.PHP_EOL;
echo PHP_EOL;

#3 - altering other references

$var3 =& $demo->example();
$var3 = 'var3 value';

echo '$demo->example() => '.$demo->example().PHP_EOL;
echo '$var1 => '.$var1.PHP_EOL;
echo '$var2 => '.$var2.PHP_EOL;
echo '$var3 => '.$var3.PHP_EOL;
echo PHP_EOL;
//  http://php.net/manual/en/language.references.return.php

function &collector() {
  static $collection = array();
  return $collection;
}
$collection = &collector();
$collection[] = 'foo';

$collection2 = &collector();
$collection2[] = 'baz';

var_dump($collection);
var_dump($collection2);

// another usage

function &collector() {
  static $collection = array();
  return $collection;
}
array_push(collector(), 'foo');
<?php

class foo {
    public $value = 1;

    public function &getValue() {
        return $this->value;
    }
}

// 1
$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 1.

$obj->value = 2;
echo $myValue; // 2

// 2
$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 1.

$myValue = 3;
echo $obj->value; // 3
TEST RUN 1:

Fruit's color is red
Fruit's color is red

TEST RUN 2:

Fruit's color is red
Fruit's color is green
$demo->example() => original value

$demo->example() => var2 value
$var1 => var1 value
$var2 => var2 value

$demo->example() => var3 value
$var1 => var1 value
$var2 => var3 value
$var3 => var3 value
Array
(
    [0] => foo
    [1] => baz
)
Array
(
    [0] => foo
    [1] => baz
)
<?php


class DataHolder {
    protected array $data = [];

    public function setData(array $data) {
        $this->data = $data;
    }

    public function &getData() {
        return $this->data;
    }
}

$dataHolder = new DataHolder();

// Nastavení dat pomocí setData()
$data = [1, 2, 3];
$dataHolder->setData($data);

// Získání dat pomocí getData()
$referenceToData = &$dataHolder->getData();

// Změna dat přímo pomocí reference
$referenceToData[0] = 100;

// Data jsou změněna i v objektu $dataHolder
var_dump($dataHolder->getData()); // Vypíše: [100, 2, 3]