Loading...

oops in php

setter and getter methods in php

  • set property visibility to private
  • define a method to set the propertys value
  • define a method to get the propertys value

Example:

<?php
class Product{
private $name;
public function set_name($value){
return $this->name=$value;
}
public function get_name(){
return $this->name;
}
}
$p=new Product;
$p->set_name('Laptop');
$p->get_name();//Laptop
  • Allow access to otherwise private properties
  • usefull to regulate access
  • useful for read only or write only properties
  • useful for pre processing values
  • avoid 'naive setter' and 'naive getter' methods