Home  • Programming • PHP

Example of Class & Object in PHP OOP

employee_class.php
<?php
class Employee{
        private$name;
        private$dept;
        private$position;
        private$salary;
	
       //Constructor Method
	 public function Employee($name,$dept,$position,$salary){
              $this->name=$name;
              $this->dept=$dept;
              $this->position=$position;
              $this->salary=$salary;
	}
	
      // Setter and Getter Methods
	 public function setName($name){
 		$this->name=$name;
		
	}
	public function getName(){
		return $this->name;
	}
		
	public function setDept($dept){
		$this->name=$dept;
		
	}
	public function getDept(){
		 return $this->dept;
 	}
	
	public function setPosition($position){
		$this->name=$position;
		
	}
	public function getPosition(){
		return $this->position;
	}
	
	public function setSalary($salary){
		$this->name=$salary;
		
	}
	public function getSalary(){
		return $this->salary;
	}
	
}//end class

?>
empolyee_demo.php
<?php
require_once("empolyee_class.php");
$faruk=new Employee("Faruk Hossain Topu","IT","Web-Developer","$2000");
	echo "Name : ".$faruk->getName();
	echo"<br/>";
	echo "Department : ". $faruk->getDept();
    echo"<br/>";
	echo "Position : ". $faruk->getPosition();
    echo"<br/>";
    echo "Salary : ". $faruk->getSalary();
    echo"<br/>";
	
	
	
	echo"<br/>";
$ratul=new Employee("Ratul","IT","Web-Designer","$1500");
	echo "Name : ".$ratul->getName();
	echo"<br/>";
	echo "Department : ". $ratul->getDept();
    echo"<br/>";
	echo "Position : ". $ratul->getPosition();
    echo"<br/>";
    echo "Salary : ". $ratul->getSalary();
    echo"<br/>";
	
	echo"<br/>";
	
	$sajid=new Employee("Sajid","IT","Web-Designer","$1500");
	echo "Name : ".$sajid->getName();
	echo"<br/>";
	echo "Department : ". $sajid->getDept();
    echo"<br/>";
	echo "Position : ". $sajid->getPosition();
    echo"<br/>";
    echo "Salary : ". $sajid->getSalary();
    echo"<br/>";
?>
Run empolyee_demo.php file from webserver

-----------------OUTPUT-------------- Name : Faruk Hossain Topu Department : IT Position : Web-Developer Salary : $2000 Name : Ratul Department : IT Position : Web-Designer Salary : $1500 Name : Sajid Department : IT Position : Web-Designer Salary : $1500

Comments 4


good
Awesome Work Bro..
Dost tomare ami amar function e ..................banaisi..............
thanks to all..
About Author
Faruk Hossain Topu
Copyright © 2024. Powered by Intellect Software Ltd