Say I have a protected method in PHP, as shown below:
class Test {
protected function hello($x) {
echo "hello " . $x . "<br>";
}
public function __construct() {
$i = 2;
self::hello($i);
$this::hello($i);
}
}
$test = new Test();
The output is hello 2
printed twice. So calling the protected method hello
works via both self and $this. Which is the correct approach? My personal answer is self, since $this refers to the current object, and any object of this class shouldn't have access to its protected method. Am I right or wrong?
Edit: This question isn't a duplicate. The other question talks only about accessing static members members through self and $this. I am interested in protected members specifically.