What is the difference between using self:: and $this-> to access class variables in PHP?
Using `self::` accesses static properties or methods in a class, while `$this->` accesses instance properties or methods. If you want to access a static property or method within a class, you should use `self::`. If you want to access an instance property or method within a class, you should use `$this->`.
class Example {
public static $staticProperty = 'static property';
public $instanceProperty = 'instance property';
public static function staticMethod() {
return self::$staticProperty;
}
public function instanceMethod() {
return $this->instanceProperty;
}
}
echo Example::staticMethod(); // Output: static property
$example = new Example();
echo $example->instanceMethod(); // Output: instance property