Spiga

Inheritance and Abstract Class Concept with PHP 5

Mayıs 23, 08 by aligorkem

Setting Up Development Environments
1.
Zend Development Environment (Zend Studio)
2. Apache Web Server (Includes Zend Studio)
3. PHP 5 (Includes Zend Studio)

Contents
1. Inheritance
2. Screenshots
3. Abstract Class Consept
3. Screenshots
4. Download sample codes

Inheritance

Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. Inheritance mechanism is used to build new classes from existing classes.

A Student has all the same features as a Person. The only difference is that the Student class have TakeExam method. If we want to create a new class, we can create another class that extends Person, We’ll call this class “Worker”.


Person.php

<?php

Class Person
{
    //Default value is empty
    private  $_name = "";
    private  $_surName = "";
   
    function  __construct( $name, $surName)
    {
        $this->_name= $name;
        $this->_surName = $surName;
    }
   
    function getName() {
       return $this->_name;
    }

    function getSurName() {
       return $this->_surName;
    }
}
?>

Student.php

<?php

require_once "Person.php";

Class Student Extends Person
{
    //Default value is empty
    private  $_result = "";

    function  __construct( $name, $surName, $result)
    {
        //parameters pass to base class
        parent::__construct($name,$surName);
       
        //We set of the values
        $this->_result = $result;
    }
   
    function __destruct()
    {
    }
    function TakeExam() {
       return $this->_result;
    }
}
?>

Index.php

<?php
    require_once "Student.php";
   
   
    $studentJake = new Student("Jake","Plummer","Failed");
    $name = $studentJake->getName();
    $surName = $studentJake->getSurName();
    $examResult = $studentJake->TakeExam();
   
    $studentJohn = new Student("John","Lennon","Succeeded");

?>

Name : <?= $name ?> <br>
SurName : <?= $surName ?> <br>
Exam Result : <?= $examResult ?> <br><br>

Name : <?= $studentJohn->getName() ?> <br>
SurName : <?= $studentJohn->getSurName() ?> <br>
Exam Result : <?= $studentJohn->TakeExam() ?> <br>

Screenshots

Abstract Class Concept

Abstract classes are used to declare common characteristics of subclasses. An abstract class cannot be instantiated. It can only be used as a superclass for other classes that extend the abstract class. An abstract class may contain abstract methods, that is, methods with no implementation.

A class abstract Person might be specified as abstract to represent the general abstraction of a Person, as creating instances of the class would not be meaningful. An abstract method is a method that is declared without an implementation.

Person.php

    
//define abstract method
abstract protected function GetCustomIDNumber();

Student.php

 
    //implement abstract method and generate random number
    function  GetCustomIDNumber()
    {
        srand((double)microtime()*1000000);
        return rand(0,100);
    }

DOWNLOAD SAMPLE CODE

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • eKudos
  • E-mail this story to a friend!

This entry no have comments... but you can be first.

Leave a Reply