Home » PHP

Inheritance in PHP :: A Simple Example

19 August 2007 153 views Popularity: 1% Share/Bookmark

email
 <?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:

  1. Simple and easy jQuery tabs with AJAX and PHP
  2. PHP: Parse Unparse String Array
  3. print_r in Javascript
  4. jQuery: A simple Slideshow
  5. PHP: Simple and easy way to format URL string
  6. Creating dynamic table in PHP : Easy and Simple tutorial
  7. Javascript: Show/Hide HTML elements
  8. Very simple add, edit, delete, display in PHP
  9. Multiple file upload with jQuery and php
  10. Displaying date and time
  • http://www.escortsmeet.com/ escorts

    A winter storm dumped snow across parts of the Midwest and headed toward the Northeast, promising to make today’s federal holiday a day for clearing sidewalks. “Presidents Day will be a great day for shoveling snow,” …