Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP OOP: Why a getter?

Tags:

oop

php

I'm a novice with OOP, and I'm having trouble grasping the necessity of a getter method.

In the example:

class foo {
    $this->bar = "test";
}

$foo = new foo();
echo $foo->bar;

What are the potential pitfalls in this situation, assuming I'm using a setter method to properly validate an updated value?

like image 636
Matthew Avatar asked Jul 05 '26 13:07

Matthew


1 Answers

The value of a getter method is that it lets you change your implementation later, but keep the interface the same.

like image 65
Ned Batchelder Avatar answered Jul 08 '26 03:07

Ned Batchelder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!