Brief PHP Scope Resolution Operator Explanation
Posted on June 23, 2015 in OOP, PHP by Matt Jennings
In the example below the scope resolution operator (::
) ensures the attack()
method of the Wizard
subclass includes the code inside the attack()
method of the Human
parent class which has the same name but is NOT overwritten.
<?php class Human { public function attack() { echo 'Human attack!<br />'; } } class Wizard extends Human { public function attack() { parent::attack(); echo 'Wizard attack!<br />'; } } $harry = new Wizard(); /* Output of code below is: Human attack! Wizard attack! */ $harry->attack(); ?>Tags: OOP