1

Considering the following class :

class MyClass {

    public function __construct($mailProvider) {
        $this->mailProvider = $mailProvider;

        echo get_class($mailProvider());
        echo get_class($this->mailProvider());
    }

}

And the following call :

$mailProvider = function () {
    $mail = new PHPMailer(true);
    return $mail;
};

$myClass = new MyClass($mailProvider);

I can't figure why the second echo would cause a call to an undefined function.

Anyone can figure it out ?

Sebastien
  • 570
  • 3
  • 5
  • 18

1 Answers1

2

Because PHP will look for the method $this->mailProvider() before looking for a property $this->mailProvider. You need to dereference the property by wrapping it in parentheses:

echo get_class(($this->mailProvider)());
markdwhite
  • 2,360
  • 19
  • 24