dongzipu7517 2018-09-04 17:37
浏览 20

使用PHP对象的'::'和' - >之间有什么区别? [重复]

This question already has an answer here:

I'm a bit confused on the different conventions for objects and accessing their functions and variables.

I know how to use -> when I'm accessing something from an object, or within an object. I know the same when I'm in an object that I can use parent::item or classname::item but I don't know much beyond I use them because they work. Would someone break these down for me and explain when and why I should use one method vs the other?

class mammal{
    public age = 7;
}

class dog extends mammal{
    public dogSpecificVal;

    public function getAge(){
        return $this->age;
        return $parent::age;
        return $mammal::age;
    }
}

$clifford = new dog();
$cliffordAge = $clifford->getAge();

In that example, I used three different methods to retrieve the age. They all work, but I don't know why or when I should use one over the other.

</div>
  • 写回答

1条回答 默认 最新

  • doutui7955 2018-09-04 17:48
    关注

    Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property. See Static Keyword for more information on the difference between static and non-static properties.

    http://php.net/manual/en/language.oop5.properties.php

    http://php.net/manual/en/language.oop5.visibility.php

    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 R语言 拟时序分析降维图如何减少分支
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统