0

hi all,
i have one question around return function
for ex.

<?php
    class Employee
    {
        private $name;
        // Getter
        public function getName() {
            return $this->name;
        }
        // Setter
        public function setName($name){
            $this->name = $name;
        }
    }
    $azer=new Employee();
    $azer->setName('sahib');
    echo $azer->getName();
?>

why here we must declare two functions. one serve as getting values and the other one serve as setting values. why i cannot use for this both getting values and setting together inside one functions. such as below.

<?php
    class Employee
    {
        private $name;
        // Getter
      
        // Setter
        public function setName($name){
            $this->name = $name;
return $this->name;
        }
    }
    $azer=new Employee();
    $azer->setName('sahib');
    echo $azer->setName();
?>

can anyone help me if possible
thanks in advance

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by azegurb
0

Of course you can do so but erro is in the following line.

echo $azer->setName();

The function excepts at least one parameter. It should be like this

$getted = $azer->setName('sahib');

echo $getted;
0

thank you very much
i did it like you thought but got nothink
can you see it again if possible
again thanks for attention

0

It is working with me..

<?php
    class Employee
    {
        private $name;
        // Getter
      
        // Setter
        public function setName($name){
            $this->name = $name;
return $this->name;
        }
    }
    $azer=new Employee();
    $getted = $azer->setName('sahib');
	echo $getted;
?>
0

one question please.
if this way is possible why is needed to create the getter function additionally

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.