PHP cannot access protected property error

$message is a protected member of class Exception, as the error message states. You want the public accessor getMessage:

$e->getMessage()

Members declared protected can be accessed only within the class itself and by inherited and parent classes.

class MyClass {
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';

    function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

You can dig more into Property Visibility here


Use $e->getMessage() instead of $e->message because message is a protected property :)

Tags:

Php