PHP OOP Practice

Without Constructors:

class_lib.php

<?php 

class person{

var $name_tame;

function set_name($new_name)
{
$this->$name_tame=$new_name;
}

function get_name()
{
return $this->name_tame;
}

}

?>

index.php

<?php include("class_lib.php"); ?>
<?php

$zaki=new person();
$riyad=new person;

//setting data to object
$zaki->set_name("Syed Ahmed Zaki");
$riyad->set_name("MH Riyad");

//Accessing an object data
echo "Zaki's full name".$zaki->get_name();
echo "Riyad's full name is:".$riyad->get_name();

?>

With constructors:

class_lib.php

<?php

class person{

var $name_tame;

function __construct($persons_name)
{

$this->name_tame=$persons_name;

}

function set_name($new_name)
$this->name_tame=$new_name;
}

function get_name()
{
return $this->name_tame();
}


?>

index.php

<!DOCTYPE html>
<html>
<head>

<title>OOP practice in PHP</title>
<?php include("class_lib.php") ?>
</head>

<body>
<?php

$zaki=new person("Syed Ahmed Zaki");
echo "Zaki's full name is:".$zaki->get_name();
?>
</body>

</html>

With modifiers:

class_lib.php

<?php

class person{
var $name_tame;
public height;
protected $social_insurance;
private $pinn_number;

function __construct($persons_name)
{
$this->name_tame=$persons_name;
}

function get_name()
{
return $this->name_tame;
}

public function set_pin($pin_tin)
{
$this->pinn_number=$pin_tin;
}

public function get_pinn_number(){
return $this->pinn_number;
}

}

?>

index.php

<?php include("class_lib.php") ?>
<?php

$zaki=new person("Syed Ahmed Zaki");
echo "Zaki's full name: ".$zaki->get_name();

$pinno=new person("Zaki");

$pinno->set_pin("12345");

echo "Tell me private stuffs:".$pinno->get_pinn_number;


?>

With Inheritance:
class_lib.php

<?php

class person{
    var $name_tame;
    public $height;
    protected $social_insurance;
    private $pinn_number;

    function __construct($persons_name){
        $this->name_tame=$persons_name;
    }

    function get_name()
    {
        return $this->name_tame;
    }

    public function set_pin($pin_tin){
        $this->pinn_number=$pin_tin;
    }

    public function get_pinn_number(){
        return $this->pinn_number;
    }

}




class employee extends person{

function __construct($employee_name){
$this->set_name($employee_name);
}

function get_name(){
return $this->name_tame;
}

}

?>

index.php

<?php include("class_lib.php") ?>
<?php
$stefan=new person("Stefan mischook");
echo "Stefan's fulle name".$stefan->get_name();

$james=new employee("Johhnyy Fingers");
echo "-->".$james->get_name();
?>

Overriding methods:

<?php

class person
{
 protected function set_name($new_name){
     if($new_name != "Jimmy Two Guns")
    {
$this->name=strtoupper($new_name);

    }

}

class employee extends person
{
   protected function set_name($new_name)
{
if($new_name=="Stefan Sucks")
{
$this->name=$new_name;
}
}
}

?>

 

 

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.