Share on Google+Share on Google+

PHP Mutator Accessor

Advertisement
In object-oriented programming, the mutator method (also called "setter") is used to initialize the member variables of a class. On the other hand accessor methods are used to get the values of the private data member.

PHP Mutator and Accessor Methods:

In object-oriented programming, the mutator method (also called "setter") is used to initialize the member variables of a class. According to the encapsulation principle the variables of the class are declared as private to protect and can be modified by a public declared function.

On the other hand accessor methods are used to get the values of the private data member.

These methods can be implemented on non-object-oriented programming language as well. In this case we have to pass the address (reference) of the variable.

PHP Mutator Method Example:

<?php

class One{

private $string;

public function mutator($arg){

$this->string=$arg;}

public function accessor(){

echo "Value is: ".$this->string;}}

$one=new One();

$one->mutator("roseindia");

$one->accessor();

?>

 

Output:

Value is: roseindia

 

Advertisements

Advertisement

Posted on: February 19, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.