What is inheritance in php with example?



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

Post New Answer

More PHP Interview Questions

How can we enable error reporting in php?

1 Answers  


What is parent __construct ();?

1 Answers  


What is php pathinfo?

1 Answers  


Explain the three different kinds of Arrays?

1 Answers  


Want to know the 10th max salary in salary table

3 Answers  


Is php coding easy?

1 Answers  


Explain Creating and Naming an Array?

1 Answers  


What is the difference between php and javascript?

1 Answers  


How can I know that a variable is a number or not using a JavaScript?

2 Answers  


What are the differences between include() and include_once () functions?

12 Answers  


How can we check the value of a given variable is a number?

1 Answers  


How can you count number of parameters given in a URL by POST method?

5 Answers  


Categories