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

Recommended Answers

All 5 Replies

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;

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

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;
?>

Thank you very much

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.