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
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
accessing methods,
Difference between accessing a class methods,
OOP interview questions and answers,
PHP,
PHP interview questions and answers
.You can leave a response, or trackback from your own site.