Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exporting Properties

By Tim Macdonald

<?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

PHP Features

Last updated: 10 September 2026