4

Possible Duplicate:
Reference - What does this symbol mean in PHP?

What exactly does -> do in php?

I have a good understanding of the basics of php but never understood this. I tend to see in apps that use Codeignitor.

Community
  • 1
  • 1
codedude
  • 6,244
  • 14
  • 63
  • 99

5 Answers5

4

It accesses accessible child methods or properties of objects:

class myClass {
  public $fizz = 'Buzz';
  public function foo() {
    echo 'Bar';
  }
}

$myclass = new myClass();
$myclass->foo(); // outputs 'bar'
$myclass->fizz = 'Not Buzz'; // overwrites $fizz value
Sampson
  • 265,109
  • 74
  • 539
  • 565
3

I'm sure theres a more technical explanation but that is used to access properties and methods of an object.

studioromeo
  • 1,563
  • 12
  • 18
2

Its basically the equivalent to the . in javascript. The both acess an Objects properties/methods.

The biggest difference is that in PHP only classes are Objects. While in JavaScript everything is an Object.

Therefore you can't do "string"->method() in php while you can do the equivalent in JavaScript "string".method().

Lime
  • 13,400
  • 11
  • 56
  • 88
1

-> is accessing a varible inside of a class, so that

$class->variableInClass

It also can work with functions, with the same syntax as above.

If you're not familiar with OOP, I'd suggest looking here

Precursor
  • 622
  • 7
  • 18
  • 1
    Not a regular user of PHP, but don't you mean object? You would use the `::` operator to access class variables. – Cristian Sanchez Jul 21 '11 at 01:25
  • "Variables inside of a class" are actually "properties of an object" ))) And with `->` not only properties can be used. – zerkms Jul 21 '11 at 01:25
0

Well I would razz you a bit because this is a very common operator. However it's very difficult to google for, so I understand.

This is a class access operator. It allows you to access members and functions of a class. So for example if I have a class named A with a member x, I could access it like this:

$a = new A();
$a->x;
Chris Eberle
  • 47,994
  • 12
  • 82
  • 119