Showing posts with label accessing methods. Show all posts
Showing posts with label accessing methods. Show all posts
Difference between accessing a class method via -> and via :: ?
Posted by Raj
What's the Difference between accessing a class method via -> and via ::?
The -> is used for accessing properties and methods of instantiated objects.
:: is used for accesing static methods or attributes and don't require to instantiate an object of the class.
Example:
<?php
class Person
{
var $name = Raj;
public function getName()
{
return $this->name;
}
public static function getAge()
{
return "25";
}
}
$person = new $person();
echo $person->getName();//Require to instantiate an object of the class.
echo person::getAge();//:: is used for accesing static methods(getAge), so don't require to instantiate an object of the class
?>
Output:
Raj 25