This a very simple class to understand php5. If you are new to OOP, it may take some time to understand but once you understand, you will Rock!Please read through cause details are given in comments.
class Employee{ private $firstName; private $lastName; private $emploeeId; /* *Variable are private because you don't want them visible from other classes. *Hence we need the public function to pull the data. */ public function __construct($fName,$lName,$eId){ $this -> firstName = $fName; $this -> lastName = $lName; $this -> employeeId = $eId; } /*some of you may get a bit confused here.Question is why need __construct()? *Answer is you need to create object.That object will hold data reference. */ public function getFirstName (){ return $this -> firstName; } public function getLastName (){ return $this -> lastName; } public function getEId (){ return $this -> employeeId; } /* All of these get() function are required to pull the *data as you has made the variable private. * If your variable was public, you could call them using *echo $a ->firstName; * But here you can not. It will show the following error: * Fatal error: Cannot access private property Employee:: */ } $a = new Employee ('Mahmudul','Mithun','000001'); /* So $a is a new object which has the values of *'Mahmudul','Mithun','000001'=>3 parameters *Which will transfer the value to the *__construct('Mahmudul','Mithun','000001') *this method will make these local variable available *to the global variable i.e private variables *So now the values are set and you can pull them using get()method. */ echo $a ->getFirstName()." ". $a ->getLastName()." ". $a ->getEId();Hope this may help you a bit to understand.Please don't forget to give me some feedback.
Post a Comment