Files
namaste/stubs/testReferences.php
gramps 373ebc8c93 Archive: Namaste PHP AMQP framework v1.0 (2017-2020)
952 days continuous production uptime, 40k+ tp/s single node.
Original corpo Bitbucket history not included — clean archive commit.
2026-04-05 09:49:30 -07:00

70 lines
1.8 KiB
PHP

<?php
/*
* this stub tests the ability to reference different class objects, via a generic class member, and still have the
* IDE access the member functions and variables of the assigned class without generating warnings or errors.
*
* 01-07-20 mks original coding
*/
class bar
{
public int $y;
public function __construct()
{
$this->y = 0;
}
public function changeY(int $newValue): bool
{
if ($newValue == $this->y) return false;
$this->y = $newValue;
return true;
}
}
class foo
{
public int $x;
public object $copy;
public function __construct()
{
$this->x = 1;
$this->copy = new stdClass();
}
public function changeX(int $newValue = 1): bool
{
if ($newValue == $this->x) return false;
$this->x = $newValue;
return true;
}
}
$original = new foo();
echo 'Value of original->x: ' . $original->x . PHP_EOL;
echo 'Value of original->x was ' . ($original->changeX(10)) ? '' : 'not ';
echo 'changed.' . PHP_EOL;
// assign the reference
$original->copy =& $original;
echo 'Value of copy->x ' . $original->copy->changeX(10) ? '' : 'not ';
echo 'changed.' . PHP_EOL;
echo 'value of original->copy->x: ' . $original->copy->x . PHP_EOL;
// reassign the object $original->copy to the class object $argle
$objectTwo = new bar();
$original->copy =& $objectTwo;
// note that even though $original->copy is decl as type object, we can still assign another object, of type argle
// to the member AND access that member's functions without generating a warning!
if ($original->copy->changeY(10))
echo 'original->copy value changed!' . PHP_EOL;
else
echo 'original->copy unchanged' . PHP_EOL;
echo 'value of original->copy->y: ' . $original->copy->y . PHP_EOL;