Exporting Properties¶
<?php
class MyList
{
private $list = [];
function &getList()
{
return $this->list;
}
function add($item)
{
$this->list[] = $item;
}
}
$list = new MyList;
$list->add(1);
$list->add(2);
$arr = &$list->getList();
$arr[] = 3;
$arr[] = 4;
dd($list->getList());
// array:4 [
// 0 => 1
// 1 => 2
// 2 => 3
// 3 => 4
// ]
With an accessor and a reference, it is possible to export a private property and manipulate it from the outside of the object.
This is not recommended, as it exposes data that is supposed to be protected by its visibility.
See Also¶
Export properties [Try me]
PHP Features¶
Last updated: 16 July 2026