Object Oriented Concepts
Object Oriented Concepts
Member Variable − These are the variables defined inside a class. This data
will be invisible to the outside of the class and can be accessed via member
functions. These variables are called attribute of the object once an object is
created.
Member function − These are the function defined inside a class and are used
to access object data.
Parent class − A class that is inherited from by another class. This is also
called a base class or super class.
Child Class − A class that inherits from another class. This is also called a
subclass or derived class.
<?php
class phpClass {
var $var1;
[..]
[..]
?>
The special form class, followed by the name of the class that you want to
define.
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(){
function setTitle($par){
$this->title = $par;
function getTitle(){
}
?>
The variable $this is a special variable and it refers to the same object ie.
itself.
Here we have created three objects and these objects are independent of
each other and they will have their existence separately. Next we will see
how to access member function and process member variables.
Constructor Functions
Constructor Functions are special type of functions which are called
automatically whenever an object is created. So we take full advantage of
this behaviour, by initializing many things through constructor functions.
Following example will create one constructor for Books class and it will
initialize price and title for the book at the time of object creation.
function __construct( $par1, $par2 ) {
$this->title = $par1;
$this->price = $par2;
}
Now we don't need to call set function separately to set price and title. We
can initialize these two member variables at the time of object creation
only. Check following example below −
$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
Destructor
Like a constructor function you can define a destructor function using
function __destruct(). You can release all the resources with-in a
destructor.
Inheritance
PHP class definitions can optionally inherit from a parent class definition by
using the extends clause. The syntax is as follows −
class Child extends Parent {
<definition body>
}
The effect of inheritance is that the child class (or subclass or derived class)
has the following characteristics −
Automatically has all the member variable declarations of the parent class.
Automatically has all the same member functions as the parent, which (by
default) will work the same way as those functions do in the parent.
Following example inherit Books class and adds more functionality based on
the requirement.
var $publisher;
function setPublisher($par){
$this->publisher = $par;
function getPublisher(){
Now apart from inherited functions, class Novel keeps two additional
member functions.