The parent operator

If you write a method in a subclass with the same name of one in its parent class, its statements will override those of the parent class. Sometimes this is not the behavior you want and you need to access the parent’s method. To do this, you can use the parent operator.


$object = new Son ();
$object->test ();
$object->test2 ();
class Dad {
	function test() {
		echo "[Class Dad] I am your Father<br />";
class Son extends Dad {
	function test() {
		echo "[Class Son] I am Luke<br />";
	function test2() {
		parent::test ();

This code creates a class called Dad and then a subclass called Son that inherits its properties and methods, then overrides the method test. Therefore, when line 2 calls the method test, the new method is executed. The only way to execute the overridden test method in the Dad class is to use the parent operator, as shown in function test2 of class Son.


[Class Son] I am Luke
[Class Dad] I am your Father

If you wish to ensure that your code calls a method from the current class, you can use the self keyword, like this:


You may also like...