See also: $this pseudo-variable, http://php.net/manual/en/language.oop5.basic.php
In addition, when inside a class method function, indirect access to object properties also works:
<?php
$name = 'departments';
$departments = $this->$name;
$this->$name = $different;
?>