-2

I'm fairy new to php, but have a lot of OOP experience. I was just wondering why you do this in php.

What I think your meant to do:

$example->functionName();

What I thought you would've done:

$example.functionName();

(Please tell me if Ive got it all wrong)

So the question is, why do you use -> instead of . in php? Or is there no difference?

Jonty Morris
  • 797
  • 1
  • 10
  • 25

2 Answers2

3

In PHP, -> is the "object operator". It's used to access a property or method of an object. There are no specialties, just that PHP decided to use this symbol.

Additionally, there is :: to access static methods and properties of classes (MyClass::staticMethod()).

. in PHP is used to concatenate strings.

ROAL
  • 1,749
  • 1
  • 14
  • 21
2

"."(DOT) is used for string concatation operation in php.

$str1='AB';
$str2='CD';
echo $str1.$str2; // ABCD
echo $str1.' '.$str2; // AB CD

While $example->functionName(); refers that $example is an object and it is calling functionName() as class method.

Simply, . is a concatenation operator and -> is an object operator.

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • Just a little note: `class method` is akin to `static method` - they operate not on the object but on the class. A method that operates specifically on a given object would be called `instance method`. – ROAL Apr 07 '16 at 10:05