Inheritance in PHP :: A Simple Example
<?php
/* creating a class with two variables and a function */
class abc
{
var $name = "Ram";
var $surname = "Sharma";</code>
function greet()
{
echo "Welcome! $this->name $this->surname";
}
}
class xyz extends abc
{
var $middlename = "Kumar";
<span id="more-13"></span>
/* the variables 'name' and 'surname' need not be defined here because they are already defined in the parent class (abc) of this class (xyz) */
function hello()
{
echo "Hello! $this->name $this->middlename $this->surname";
}
}
$st = new xyz();
$st->hello();
echo "<br/>";
/* since the class 'xyz' inherits the class 'abc', the function 'greet' of the class 'abc' will work fine with the object(st) of the class 'xyz' */
$st->greet();
?>
Result:
Hello! Ram Kumar Sharma
Welcome! Ram Sharma
Related posts:
- Simple and easy jQuery tabs with AJAX and PHP
- PHP: Parse Unparse String Array
- print_r in Javascript
- jQuery: A simple Slideshow
- PHP: Simple and easy way to format URL string
- Creating dynamic table in PHP : Easy and Simple tutorial
- Javascript: Show/Hide HTML elements
- Very simple add, edit, delete, display in PHP
- Multiple file upload with jQuery and php
- Displaying date and time
