2

I have seen this at a lot of place but have never understood it's meaning or working... for example:

// Registry
$registry = new Registry();

// Loader
$loader = new Loader($registry);
$registry->set('load', $loader);

If someone can elaborate this, I will be very greatful... thanks in advance...

Charles
  • 50,943
  • 13
  • 104
  • 142
mas2040
  • 31
  • 1
  • Depending on your familiarity with C++, you may find it useful to know that it is basically identical to C++'s -> – Cam Jun 07 '10 at 08:26
  • *(sidenote)* the correct name of the parser token is `T_OBJECT_OPERATOR` - http://php.net/manual/en/tokens.php – Gordon Jun 07 '10 at 08:26
  • Also see http://stackoverflow.com/questions/1671602/what-is-the-story-on-variable-something-in-php/1671613 – Mike B Jun 07 '10 at 11:17

4 Answers4

8

That calls a method on an instance of a class or accesses a field of an instance of a class. You may find this page useful.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • it's like registry.set() in java, ya? – pxl Jun 07 '10 at 08:43
  • Do you know how you would call it verbally. Like for instance, while talking, if I am referring to the AsyncTask.onProgressUpdate method, I can say, "just call AsyncTask dot onProgressUpdate. But if it is this->doSomething(), then how would I refer to it in colloquial speech? – Arunabh Das Aug 18 '11 at 13:45
  • @Arunabh: I'd say something like "call `doSomething` on `this`". If you need to refer to `->` specifically, you might be able to say "arrow" (although that might not be standard terminology). – icktoofay Aug 19 '11 at 03:09
3

Its use is in object oriented programming. Consider the following:

class myclass {

    public $x;

    private $y;

    public function get_y () {
        return $this->y;
    }

    public function __construct ($new_x, $new_y) {
        $this->x = (int)$new_x;
        $this->y = (int)$new_y;
    }

}

$myobject = new myclass(5, 8);

echo $myobject->x; // echoes '5';

echo "\n";

echo $myobject->get_y(); // echoes '8'

echo $myobject->y; // causes an error, because y is private

You see how it is used to refer to the properties of an object (the variables we specified in the class definition) and also the methods of the object (the functions). When you use it in a method, it can point to any property and any method, but when used outside the class definition, it can only point to things that were declared "public".

Hammerite
  • 21,755
  • 6
  • 70
  • 91
0

This is heritage from C++, an access to member (method or attribute) by pointer.

For more C++ operators look here in wikipedia

Community
  • 1
  • 1
João
  • 2,296
  • 5
  • 20
  • 30
0

// Registry $registry = new Registry();

// Loader $loader = new Loader($registry); $registry->set('load', $loader);

Here $registry->set('load', $loader); is the call to the function set('load',$loader), which is is defined in Registry Class.

Also $registry is the instance or object of Registry() Class. Hence using this object, set() function of Registry Class is called.

Neeraj
  • 483
  • 9
  • 17