Hi,

Code below is just to print a message. Nothing more. Question is: Whether we have the line return $this->myname; or not, it works fine but why some people still use return in such cases? Is it just a matter of taste or I'm missing something about OOP? Example below might look silly but I just wanted to explain what I meant.

Thanks in advance

    class Whatever
    {
        var myname = null;

        private function set_my_name($name)
        {
            $this->myname = $name;

            return $this->myname;
        }

        public function say_hello($name)
        {
            $this->set_my_name($name);

            echo 'Hello ' . $this->myname;
        }
    }

    $obj = new Whatever();
    $obj->say_hello('John');
Member Avatar for diafol

Sorry, misread your post :(

You could use return statement to return true on success and false on failure. This way you can test the method when calling it from somewhere else like:

$we = new Whatever;

if($we->say_hello('Jack')) {
    // do something like log success
    // ...
} else {
    // do something else like log failure
    // ...
}

The say_hello() function can be changed to:

public function say_hello($name)
{
    if($name != '')
    {
        $this->set_my_name($name);
        echo 'Hello ' . $this->myname;
        return true;
    } else {
        return false;
    }
}

Maybe not the best example but you get the idea. Returning anything is not mandatory of course.

DISCLAIMER! This is just an opinion from a 19yo.

I don't see the necessity of using return $this->myname;.. if this is going to be use by other methods below it.Otherwise, when it is needed to be somewhere else without the hello.

Most coders use the same style, but ONLY for the purpose of method chaining. However, the approach is completely different from your codes above, because it only return->$this and not the whole thing..

Here is a sample code for method chaining.. Zend uses this a lot... I also see this a lot on my older brothers source codes, up to the extent of chaining multiple classes..

   <?php
    class Whatever{
     private $name = null;
     private $hello = null;

    public function setName($name){
     ## do something with the name
     $this->name = trim($name);
     return $this;

    }

    public function say_hello($hello){

    $this->hello = $hello;
    return $this;

    }
    public function makeSenseOutOfThis(){

        echo "This is an output of method chaining ->".$this->hello." to ".$this->name;

    }

}

$aName = new Whatever();

$aName->say_hello('hello')->setName('SomeName')->makeSenseOutOfThis();
?>

for your sample codes above, I prefer doing it this way.., because it gives you more option to extends to whatever functions, conditions in the future..

<?php
    class Whatever{
     private $name;

     public function __construct($name = null){
        $this->name = $name;
    }


    public function setName(){
     ## do something with the name
     ## this trimming can also be done within the constructor.
     $this->name = trim($this->name);
     ## I hate returning from the setter but should return from the getter

    }

    public function getName(){
     ## 
     return $this->name;

    }

    public function sayHello($hello){
        $this->hello = $hello;

        return $this->hello." ".$this->name;

    }
}

$aName = new Whatever('SomeName');

echo $aName->getName();
echo "<br/>";
echo $aName->sayHello('hello');
?>

Seing people using return $this->myname part was making me think that it was a kind of "COMMIT" statement like we have have in DB queries. If the line $this->myname = $name; sets and reflects the changes instantly then there is no point of using return $this->myname. Just an extra unnecessary line.

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.