adspace


What is inheritance in php with example?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is trait in php?

1065


What is the current stable version of php? What advance thing in php7?

1093


Explain me is it possible to destroy a cookie?

1025


List some features of php that are deprecated in php7?

1020


What is the difference between htmlentities() and htmlspecialchars()?

1089


sort term descripttion form, report and uery

2254


Write a program using while loop?

1115


How to calculate the difference between two dates using php?

1142


What is difference between static and final in php?

1107


Which variable declarations within a class is invalid in php?

1046


hello all, I need some sample placement papers in lion bridge.. can anyone help me?

2174


How to create a web form?

1108


Name and explain five of the PHP error constants?

1032


How can we extract string "pcds.co.in" from a string "https://info@pcds.co.in" using regular expression of php? More on reg can you explain

1076


how to detect a mobile device using php

1172