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