SQL

Saturday, 17 March 2012

Getter and Setter in PHP


In PHP class getter and setter methods are really useful to show the results or pull the data.When you create a value as public it is visible from everywhere.That means inside or outside the class and any one can pull the data or extend the class to change it.So here comes the private or protected privileges.set() sets the value and get() gets the value.So you can pull the data using the get().Here is a simple example:

class Employee
{
// properties defined here
protected $_name = 'John';
// methods defined here
// methods defined here
public function getName()
{
return $this->_name;
}
public function setName($name)
{
$this->_name = $name;
}

}

Now create another test.php and copy followings:
// include the class file
require_once 'Employee.php';
// create an instance of the Employee class
$employee = new Employee();
// display the $_name property
//echo "$employee->_name";
$employee->setName('Robert');
echo $employee->getName();


If you run the file you can see Robert but may be you are expecting John.This is because we have set the variable to Robert using set().

Setting default values with a constructor method

A constructor builds the object, applying default values and assigning to properties values passed to the class when an object is instantiated.
the constructor for all classes is called __construct()

The constructor works like a setter method, so any values passed to it as arguments can be
assigned to properties by using $this to refer to the current object like this:
public function __construct($value)
{
$this>property = $value;
}

Lets create a constructor with 2 properties:

// constructor
public function __construct($name, $title)
{
$this->_name = $name;
$this->_title = $title;
}

Because the value of $_name is set by the constructor, you no longer need the
setter method. So we can delete it and add a getter method to retrieve the value of the
$_title property. The Employee.php should look
like this:

class Employee
{
// properties defined here
protected $_name;
protected $_title;
// constructor
public function __construct($name, $title)
{
$this->_name = $name;
$this->_title = $title;
}
// methods defined here
public function getName()
{
return $this->_name;
}


public function getTitle()
{
return $this->_title;
}
}

the class constructor now expects two arguments. To keep the code simple, I haven’t added any code to check the type of data passed as arguments, but let’s assume that both are meant to be strings. Test the revised class by the code in test.php as follows
require_once 'employee.php';
$employee1 = new Employee('John', 'Manager');
$employee2 = new Employee('Robert', 'Cashier');
echo '$employee1 is  ' . $employee1->getName();
echo ' Title is "' . $employee1->getTitle() . '"
';
echo '$employee2 is  ' . $employee2->getName();
echo ' Title is  "' . $employee2->getTitle() . '"
';

When you run test.php, you should see something similar to



$employee1 is John Title is "Manager"
$employee2 is Robert Title is "Cashier"



Post a Comment