It's different. When you are using :: it means you are dealing directly with class. when you are using -> you are dealing with object. I change your sample to show you how it works:
class B
{
private static $var;
public static function test() {
echo self::$var."Static function called<br/>";
}
public static function change($value)
{
self::$var = $value;
}
}
B::test();
$ob = new B();
$ob->test();
$ob->change(10);
B::test();
the result will be:
Static function called
Static function called
10Static function called
because the first time static $var doesn't have anything in it. it shows just string. after creating $ob object we changed STATIC $var to 10. and after that because it was STATIC, when you are using :: again, you can see that static var changed.
now see another example:
class B
{
private static $var;
public function test() {
echo self::$var."function called<br/>";
}
public function change($value)
{
self::$var = $value;
}
}
B::test();
$ob = new B();
$ob->test();
$ob->change(10);
B::test();
The result will be:
function called
function called
10function called
because as i said before, :: means you are directly dealing with class.
now see another example:
class B
{
private $var;
public function test() {
echo $this->var."function called<br/>";
}
public function change($value)
{
$this->$var = $value;
}
}
$ob = new B();
$ob->test();
$ob->change(10);
B::test();
the result will be:
function called
Fatal error: Uncaught Error: Cannot access empty property
When you create $ob you have object. when you are using :: you can not have $var inside test or change method. because they have $this-> in it and it means they are dealing with object.
and when you know these, you know when and how must use :: or ->