Home Tutorial Php Phpoop PHP Mutator Accessor

 
 

PHP Mutator Accessor
Posted on: February 19, 2010 at 12:00 AM
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

 

Related Tags for PHP Mutator Accessor:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.