PHP Mutator Accessor


 

PHP Mutator Accessor

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.

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

 

Ads