Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [Solved] Arrow Operator Re: [Solved] Arrow Operator

#72965
Democritus
Member

The arrow is an accessor in object oriented php. Here’s a simple example:

Code:
class MyClass {
public $h = “Hello”;
private $w = “World”;
function getStatement() {
echo $h . “, ” . $w;
}
}

And now, to show you how the arrow works:

Code:
$myVar = new MyClass();

So $myVar is an instance of the class above.

Code:
echo $myVar->h

This is valid, because $h is a public variable in MyClass. Therefore, $myVar->h accesses the value of $h stored in this instance of MyClass.

Code:
echo $myVar->w

This will cause a runtime error, because w is private. You cannot access private variables directly with the -> accessor.

Finally,

Code:
$myVar->getStatement();

will echo "Hello, World", because the method is public (public is default if not explicitly declared). MyClass has access to its own variables, so World is printed out in this case, even though we couldn’t directly access it with:

Code:
$myVar->w

This answer assumes you understand object oriented concepts at least a little bit. If you don’t, head on over to http://www.php.net/manual/en/language.oop5.basic.php.

Best of luck,
-D