What's the difference between :: (double colon) and -> (arrow) in PHP?

前端 未结 6 1232
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 16:54

There are two distinct ways to access methods in PHP, but what\'s the difference?

$response->setParameter(\'foo\', \'bar\');

and

6条回答
  •  無奈伤痛
    2020-11-22 17:38

    Yes, I just hit my first 'PHP Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM'. My bad, I had a $instance::method() that should have been $instance->method(). Silly me.

    The odd thing is that this still works just fine on my local machine (running PHP 5.3.8) - nothing, not even a warning with error_reporting = E_ALL - but not at all on the test server, there it just explodes with a syntax error and a white screen in the browser. Since PHP logging was turned off at the test machine, and the hosting company was too busy to turn it on, it was not too obvious.

    So, word of warning: apparently, some PHP installations will let you use a $instance::method(), while others don't.

    If anybody can expand on why that is, please do.

提交回复
热议问题