Posts

Showing posts from July, 2016

Class & Object in PHP

Example Here is an example which defines a class of Books type − php class Books { /* Member variables */ var $price ; var $title ; /* Member functions */ function setPrice ( $par ){ $this -> price = $par ; } function getPrice (){ echo $this -> price . " " ; } function setTitle ( $par ){ $this -> title = $par ; } function getTitle (){ echo $this -> title . " " ; } } ?> The variable  $this  is a special variable and it refers to the same object ie. itself. Creating Objects in PHP Once you defined your class, then you can create as many objects as you like of that class type. Following is an example of how to create object using  new operator. $physics = new Books; $maths = new Books; $chemistry = new Books; Here we have created three objects and these objects are indep