Monday, September 7, 2015

object oriented programming in php :: inheritance

<?php

function is_phone_number($number){
    $length = strlen($number);
    if($length == 11){

        for ($i=0; $i < $length; $i++) {
            if(ord( $number[$i]) < ord('0') ||  ord( $number[$i]) > ord('9')){
                return false;
            }
        }
        return true;
    }
    return false;
}

class Person {
    public $name;
    public $profession;
    public $phone_number;
    public function check_phone_number($phone_number){
        if(is_phone_number($phone_number)){
            $this->phone_number = $phone_number;
        }
    }
}

class Guest extends Person{
    public $indian_phone_number;
    public function check_indian_phone_number($phone_number){
        if(is_phone_number($phone_number . '0')){
            $this->indian_phone_number = $phone_number;
        }
    }
}

//$indian = new Guest();
//$indian->name = 'Daniel';
//$indian->profession = "Soft skill trainer";
//$indian->check_phone_number('01670978989');
//$indian->check_indian_phone_number('0123456789');
//print_r($indian);
$indian2 = new Guest();
$indian2->name = 'Vasanth';
$indian2->profession = 'Trainer';
$indian2->check_phone_number('01670978989');
$indian2->check_indian_phone_number('8008900910');
print_r($indian2);

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }