-1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Sly
  • 763
  • 1
  • 10
  • 31
  • 1
    One is static (`::`) and one is instantiated (`->`). It mostly boils down to two different ways to get the same information, but there are some differences and reasons to pick and choose which one. Here are some links that explain the difference between the two, and when to use them: [When to use static vs instantiated](https://stackoverflow.com/questions/1185605/when-to-use-static-vs-instantiated-classes), [Static vs Instance method](https://stackoverflow.com/questions/30402978/php-static-vs-instance-method) – aynber Sep 04 '18 at 17:40
  • 1
    Check this link https://stackoverflow.com/questions/1224789/php-classes-when-to-use-vs/ – Yousaf Sep 04 '18 at 17:45

1 Answers1

0

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

dhayzon
  • 11
  • 3