开发者

What does "->" do or mean in PHP?

开发者 https://www.devze.com 2023-01-02 07:34 出处:网络
I have seen this at a lot of place but have never understood it\'s meaning or working... for example:

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 c开发者_运维问答an elaborate this, I will be very greatful... thanks in advance...


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.


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".


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

For more C++ operators look here in wikipedia


// 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消