What is the difference between accessing a class method via & # 8594; and through ::?

What is the difference between accessing a class method via → and via ::?

+5
source share
4 answers

->designed to access the properties and methods of created objects. ::- This is access to static methods, constants, or overridden methods.

For more information:

+6
source

:: It is used to access static methods or attributes, so you do not need to instantiate an object of the containing class.

-> used to access methods or attributes of created objects.

+4
source

-> ( , , / ).

:: - , , , .


:

class Mustang
{
  var $gallons = 12; // gallons

  public function getFuel()
  {
    return $this->gallons;
  }

  public static function getEngine()
  {
    return "V8";
  }
}

$mustang = new Mustang(); // creating an instance

echo $mustang->getFuel(); // retrieve the fuel (instance, _this_ mustang)

echo Mustang::getEngine(); // echo a stat about Mustangs in general (static)

"", () ( ->).

-, - , , ( V8, ::).

+2

. :: .... ( ), -. , . , . ; . ( , - , ->

, ->? , . , . :

$john = new User(); //create the object
$john->age = 10; //accessing an object property
$age = $john->getAge(); //accessing an object method
+1

All Articles