-6

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

So I've been reading through the book PHP Solutions, Dynamic Web Design Made Easy by David Powers. I read through the short section on Object Oriented PHP, and I am having a hard time grasping the idea of the -> operator. Can anyone try to give me a solid explanation on the -> operator in OOP PHP?

Example:

$westcost = new DateTimeZone('America/Los_Angeles');
$now->setTimezone($westcoast);

Also,a more general example:

$someObject->propertyName
Community
  • 1
  • 1
oman9589
  • 3
  • 8
  • 4
    it's just referencing a method or property of an object. Other languages use a dot for this operator. PHP couldn't because they used dot for string concat, hence the `->` instead. – Spudley Sep 16 '12 at 15:35
  • 1
    @Spudley: just because it's simple to you, it may not be as clear-cut for other people :) – Evert Sep 16 '12 at 15:37
  • 1
    Oh, so it's equivalent to in java, 'System.out.println("hello");' – oman9589 Sep 16 '12 at 15:42
  • @oman9589 Exactly. In PHP, that'd be `$system->out->println("hello")` (if we assume that `$system` is an object, and that `$system->out` is an object as well) – h2ooooooo Sep 16 '12 at 15:46

3 Answers3

0

The -> operator in PHP refers to either a function or a variable inside a class.

<?php
    class Example {
        public $variableInClass = "stringContent";
        public function functionInClass() {
            return "functionReturn";
        }
    }

    $example = new Example();
    var_dump($example->variableInClass); //stringContent
    var_dump($example->functionInClass()); //functionReturn
?>

Do note that if we're talking about static classes (different purpose), you use :: instead:

<?php
    class Example {
        public static $variableInClass = "stringContent";
        public static function functionInClass() {
            return "functionReturn";
        }
    }

    var_dump($example::$variableInClass); //stringContent
    var_dump($example::functionInClass()); //functionReturn
?>
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
0

$someObject->propertyName can be read as:

return value stored in propertyName from object $someObject

$someObject->methodName() can be read as:

execute methodName from object $someObject

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
-1

Classes and objects 101:

A class is defined as such:

class MyClass {

   public $value1;

   public function getValue() {

       return $this->value;

   }

}

We now defined a class with a single property, and a single function. To use these, we need to create an 'instance' of this object:

$myObject = new MyClass();

To use the property or function, we use the -> operator:

echo $myObject->value1;
echo $myObject->getValue();

Put a little bit more abstractly.. the function getValue is defined in this object. By using the -> operator on an instance of our class, what PHP does is effectively just call the function, just like any other function.. but before it gets called $this is assigned to the current object.

Hope this helps, if not.. I would simply recommend reading about OOP basics.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • 1
    Not that `$this->value` would never return anything except for an error as it's not defined anywhere. It's probably just a typo though. :) – h2ooooooo Sep 16 '12 at 15:38