Simply put, getter in PHP is just a method that allows other parts of your code to access a specific property of the class .
For instance:
<?php class Person { private $name; public function __construct($name) { $this->name = $name; }
Perhaps the method need not be designed solely to return a property; you can create other methods so that your class can do funky things .
To extend the example above, give the Person class a method called say() , and give it a function / method parameter representing what to say:
public function say($what) { printf('%s says "%s"', $this->name, $what); }
And call it after creating the object outside the class:
$bob = new Person('Bob'); echo $bob->getName(), "\n"; // Bob $bob->say('Hello!'); // Bob says "Hello!"
Notice that inside the say() method, I refer to $this->name . This is normal since the $name property is in the same class. The purpose of the getter (and its corresponding installer, if any) is to provide other parts of your code with access to this property.
source share