Vertisan Vertisan - 1 month ago 4
PHP Question

PHP - Access to variables in class

At this moment, I have somethink like this:

* Fields, which are selected from database for collections of acreditations
* @var array
* @access private
private $selectFields = [ 'firstname', 'lastname', 'additional_info' ];

* @return array
private function get_selectFields() {
return $this->selectFields;

Next in method's I do ex. this:

public function myFunction() {
var_dump( $this->get_selectFields() );

It's good practice or I should change somethink?


Code with having private field and private method that simply returns that field doesn't make sense at all. In general private modifier is used to limit access to internal properties of one class from another class. In your case you allow to get access to $selectFields only for object initiated from this class internally. There is no public interface to get $selectFields. Therefore $this->get_selectFields() and $this->selectFields are logically equal because they both return data stored in $selectFields.

Conclusion: get_selectFields is redundant and should be removed until it will become public or start to process/modify $selectFields before return.