简体   繁体   中英

what does this mean in PHP ()->

I've seen this use of syntax a few times on the Yii framework. I tried searching for an explanation but got no examples. A link will be nice if possible. It goes something like class::model()->function();

My understanding is that model is an object of class therefore it can access the function. So I tried to code it but I get " Call to a member function sound() on a non-object in". Here's my code

class animal
{
    private static $obj;

    public static function obj($className = __CLASS__)
    {        
        return self::$obj;
    }

    public static function walk()
    { 
        return "walking"; 
    } 
}


include('animal.php');

class cat extends animal
{
    public static function obj($className = __CLASS__)
    {
        return parent::obj($className);
    }

    public static function sound()
    {
        return "meow";
    }
}

echo cat::obj()->sound();

Also what benefits does it have?

That is called an object operator and this, -> , calls a class method from your created object which is defined in that class.

Here is an explanation and some examples.

$obj = new Class; // Object of the class
$obj->classMethod(); // Calling a method from that class with the object
echo cat::obj()->sound();

This will display the output of the sound() method, called on the object that is returned from cat::obj() .

The reason it's failing for you is because cat::obj() is not returning a valid object.

And the reason for that is because the obj() method returns the static obj property, but you're not actually setting that obj property anywhere.

The pattern you're trying to use here is known as a "Singleton" object. In this pattern, you call an obj() method to get the single instance of the class; every call to the method will give you the same object.

However the first call to the method needs to instantiate the object; this is what you're missing.

public static function obj($className = __CLASS__){
    if(!static::$obj) {static::$obj = new static;}
    return static::$obj;
}

See the new line where the object is created if it doesn't exist.

Also note that I've changed self to static . The way you're using this with class inheritance means that you probably expect to have a different static object for each class type, self will always return the root animal::$obj property, whereas static will return the $obj property for whichever class you're calling it from.

There are a few other bugs you need to watch out for too. For example, you've defined the sound() method as static , but you're calling it with -> , so it shouldn't be static.

Hope that helps.

The cat::obj() returns you a object of the type cat. With ->sound(); you're executing the function sound() from the class cat. All that should return "meow".

cat::obj() returns an object; ->sound(); execute a method of this object. Equivalent is

$o = cat::obj();
$o->sound();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM