What is inheritance in php with example?
Answer / Ajay Kumar Kushwaha
In PHP, inheritance is a process where one class acquires the properties and methods of another. This is done by declaring a class as a subclass (child) of another class (parent). Here's an example:
```php
// Parent class
class Animal {
public $name;
function __construct($name) {
$this->name = $name;
}
function eat() {
echo $this->name . ' is eating.';
}
}
// Child class
class Dog extends Animal {
function bark() {
echo $this->name . ' is barking.';
}
}
$myDog = new Dog('Rex');
$myDog->eat(); // Rex is eating.
$myDog->bark(); // Rex is barking.
| Is This Answer Correct ? | 0 Yes | 0 No |
How can we enable error reporting in php?
What is parent __construct ();?
What is php pathinfo?
Explain the three different kinds of Arrays?
Want to know the 10th max salary in salary table
Is php coding easy?
Explain Creating and Naming an Array?
What is the difference between php and javascript?
How can I know that a variable is a number or not using a JavaScript?
What are the differences between include() and include_once () functions?
How can we check the value of a given variable is a number?
How can you count number of parameters given in a URL by POST method?