How to Use the Parent Keyword in PHP: Syntax and Examples
In PHP, the
parent keyword is used inside a child class to call a method or access a property from its parent class. It helps you reuse or extend functionality by referring to the parent class's version of a method or constructor.Syntax
The parent keyword is used with the scope resolution operator :: to call a parent class method or constructor from a child class.
parent::methodName()calls a method from the parent class.parent::__construct()calls the parent class constructor.
php
<?php class ParentClass { public function greet() { echo "Hello from Parent!\n"; } public function __construct() { echo "Parent constructor called.\n"; } } class ChildClass extends ParentClass { public function greet() { parent::greet(); // Calls parent method echo "Hello from Child!\n"; } public function __construct() { parent::__construct(); // Calls parent constructor echo "Child constructor called.\n"; } } ?>
Example
This example shows how a child class uses parent to call the parent class constructor and a method. It demonstrates extending behavior while reusing parent code.
php
<?php class Animal { public function __construct() { echo "Animal created.\n"; } public function sound() { echo "Some generic animal sound.\n"; } } class Dog extends Animal { public function __construct() { parent::__construct(); // Call Animal constructor echo "Dog created.\n"; } public function sound() { parent::sound(); // Call Animal sound echo "Bark!\n"; } } $dog = new Dog(); $dog->sound(); ?>
Output
Animal created.
Dog created.
Some generic animal sound.
Bark!
Common Pitfalls
Common mistakes when using parent include:
- Calling
parentoutside a class that extends another class causes an error. - Forgetting to use
parent::with the scope resolution operator. - Trying to access private parent methods or properties with
parentwill fail because private members are not accessible in child classes.
php
<?php class Base { private function secret() { echo "Secret\n"; } } class Derived extends Base { public function reveal() { // parent::secret(); // This will cause a fatal error because secret() is private echo "Cannot access private method of parent.\n"; } } $obj = new Derived(); $obj->reveal(); ?>
Output
Cannot access private method of parent.
Quick Reference
| Usage | Description | Example |
|---|---|---|
| Call parent method | Calls a method from the parent class | parent::methodName() |
| Call parent constructor | Calls the parent class constructor | parent::__construct() |
| Access parent property | Access a protected or public property from parent | $this->property (no parent keyword) |
| Invalid access | Cannot access private parent members | parent::privateMethod() causes error |
Key Takeaways
Use
parent::methodName() to call a parent class method inside a child class.Call the parent constructor with
parent::__construct() to initialize inherited properties.The
parent keyword only works inside classes that extend another class.You cannot access private methods or properties of the parent class using
parent.Always use the scope resolution operator
:: with parent.