Joe_Mez
Joe_Mez

Reputation: 15

How do class methods return in php

I'm not particularly familiar with php, and I'm having trouble understanding what's happening. Using a condensed skeleton of what I have:

class Helper
{
    public function __construct($value)
    {
         $this->value = $value;
         //etc
    }

    private function prefix($val)
    {
         return '1234' . $val;
    }

    private function otherFunction()
    {
        $this->value->someFunction(function ($err, $result) {
            if($err !== null) {
                echo $err->getMessage();
            }
            return $result;
        });
    }

    public function help()
    {
        echo $this->prefix('5678'); //outputs 12345678
        echo is_null($this->otherFunction()); //outputs 1
    }
}

Why does otherFunction return null? I can echo $result right before the return and see what I'm expecting, but it's null afterwards.

Upvotes: 0

Views: 46

Answers (1)

vich
vich

Reputation: 292

Your otherFunction() doesn't return anything. Probably you echo $result inside of the callback function passed to $this->value->someFunction().

Add return just before $this->value->someFunction(... to return the value from otherFunction().

Upvotes: 3

Related Questions