Pages

Showing posts with label PHP OOPS. Show all posts
Showing posts with label PHP OOPS. Show all posts

Saturday, October 20, 2012

PHP - Classes And Inheritance


OOP stands for Object-Oriented Programming. It is a programming concept that caught on in the 1990's. OOP focuses on 'objects' which are, well, objects. They have certain characteristics, and can behave in certain ways. OOP programming has a few concepts that define it. One of the defining features we will start with is called a class.

A class shows what an object has and can do, and it consists of members. Members can be divided into properties and methods. Properties are the characteristics of the object. For example, cheese (object) has the properties of type (maybe Gorgonzola, or cheddar), color (green, or white), and flavor (awful or delicious). Methods are the actions and behaviors the object can do. For example, cheese (object) can mold. Now let's see the technical side of it.

class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these 3 properties

// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}

function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}

You declare a class by using the word 'class'. It's common to define the properties first. You define properties by using 'var'. Next the methods are defined. When using any of the properties in your methods, you use the $this keyword. If I want to use the "flavor" property in a function, I would put $this->flavor.

Now let's see this class in action.

<?php

class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these 3 properties

// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}

function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}

$zargento = new Cheese; // Zargento is a brand of cheese

// We will now give it characteristics
$zargento->giveDetails("Gorgonzola", "Awful", "Green and white");

// Now let's see those details
echo $zargento->showType();
echo "<br>"; // It seems DIC likes to get rid of my HTML br tags
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();

?>


You declare a class by using the word 'class'. It's common to define the properties first. You define properties by using 'var'. Next the methods are defined. When using any of the properties in your methods, you use the $this keyword. If I want to use the "flavor" property in a function, I would put $this->flavor.

Now let's see this class in action.

You make a new object by using 'new'. $zargento is now a Cheese object. It has access to all the properties and methods we outlined in the class. If we want $zargento to use the giveDetails() function, you use '$zargento->giveDetails()' as was used above. If you run the above script, the output will be:


Gorgonzola
Awful
Green and white

Now that you've gotten a good idea of how classes work, we can move one step further with another concept of OOP called inheritance. This allows you to create new classes using already created classes. Here is an example:


class MoreCheese extends Cheese {
var $cost;

function giveCost($f) {
$this->cost = $f;
}

function showCost() {
return $this->cost;
}
}

You use 'extends' to grab the methods and properties from the Cheese class and add them to the MoreCheese class. Let's see the full code.

<?php

class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these 3 properties

// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}

function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}

class MoreCheese extends Cheese {
var $cost;

function giveCost($f) {
$this->cost = $f;
}

function showCost() {
return $this->cost;
}
}

$zargento = new MoreCheese;
$zargento->giveDetails("Gorgonzola", "Awful", "Green and white");
$zargento->giveCost("23.39");
echo $zargento->showType();
echo "<br>";
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();
echo "<br>";
echo $zargento->showCost();

?>

As you can see, even though $zargento is no longer Cheese, and is now MoreCheese, it still retains all of the methods and properties from Cheese because the MoreCheese class inherits all of them from Cheese. The advantages to inheritance are that you don't have to edit the base class (in this case Cheese). You can also override code, as this example shows.

<?php

class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these 3 properties

// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}

function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}

class MoreCheese extends Cheese {
var $cost;

function giveDetails($q) {
echo $q;
}

function giveCost($f) {
$this->cost = $f;
}

function showCost() {
return $this->cost;
}
}

$zargento = new MoreCheese;
$zargento->giveDetails("dilly");
$zargento->giveCost("23.39");
echo $zargento->showType();
echo "<br>";
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();
echo "<br>";
echo $zargento->showCost();

?>

As you can see, the giveDetails() function of the base class Cheese has been overrided by the giveDetails() function of the MoreCheese class. This is a very useful feature of inheritance.

Thank you for reading this part of the tutorial, and I hope you learned something

Sunday, May 13, 2012

PHP OOPS Interview Questions & Answers


1) Explain what is object oriented programming language?
Object oriented programming language allows concepts such as modularity, encapsulation, polymorphism and inheritance.  Objects are said to be the most important part of object oriented language. Concept revolves around making simulation programs around an object. Organize a program around its data (object)& set well define interface to that data. i.e. objects and a set of well defined interfaces to that data. OOP is the common abbreviation for Object-Oriented Programming.  OOps have many properties such as DataHiding,Inheritence,Data Absraction,Data Encapsulation and many more.
2) Name some languages which have object oriented language and characteristics?
Some of the languages which have object oriented languages present in them are ABAP, ECMA Script, C++, Perl, LISP, C#, Tcl, VB, Ruby, Python, PHP, etc. Popularity of these languages has increased considerably as they can solve complex problems with ease.
3) Explain about UML?
UML or unified modeling language is regarded to implement complete specifications and features of object oriented language. Abstract design can be implemented in object oriented programming languages. It lacks implementation of polymorphism on message arguments which is a OOPs feature.
4) Explain the meaning of object in object oriented programming?
Languages which are called as object oriented almost implement everything in them as objects such as punctuations, characters, prototypes, classes, modules, blocks, etc. They were designed to facilitate and implement object oriented methods.
5) Explain about message passing in object oriented programming?
Message passing is a method by which an object sends data to another object or requests other object to invoke method. This is also known as interfacing. It acts like a messenger from one object to other object to convey specific instructions.
6) State about Java and its relation to Object oriented programming?
Java is widely used and its share is increasing considerably which is partly due to its close resemblance to object oriented languages such as C++. Code written in Java can be transported to many different platforms without changing it. It implements virtual machine.
7) What are the problems faced by the developer using object oriented programming language?
These are some of the problems faced by the developer using object oriented language they are: -
a) Object oriented uses design patterns which can be referred to as anything in general.
b) Repeatable solution to a problem can cause concern and disagreements and it is one of the major problems in software design.
8 ) State some of the advantages of object oriented programming?
Some of the advantages of object oriented programming are as follows: -
a) A clear modular structure can be obtained which can be used as a prototype and it will not reveal the mechanism behind the design. It does have a clear interface.
b) Ease of maintenance and modification to the existing objects can be done with ease.
c) A good framework is provided which facilitates in creating rich GUI applications.
9 ) Explain about inheritance in OOPS?
Objects in one class can acquire properties of the objects in other classes by way of inheritance. Reusability which is a major factor is provided in object oriented programming which adds features to a class without modifying it. New class can be obtained from a class which is already present.

10) Explain about the relationship between object oriented programming and databases?
Object oriented programming and relational database programming are almost similar in software engineering. RDBMS will not store objects directly and that’s where object oriented programming comes into play. Object relational mapping is one such solution.
11) Explain about a class in OOP?
In Object oriented programming usage of class often occurs. A class defines the characteristics of an object and its behaviors. This defines the nature and functioning of a specified object to which it is assigned. Code for a class should be encapsulated.
12) Explain the usage of encapsulation?
Encapsulation specifies the different classes which can use the members of an object. The main goal of encapsulation is to provide an interface to clients which decrease the dependency on those features and parts which are likely to change in future. This facilitates easy changes to the code and features.
13) Explain about abstraction?
Abstraction can also be achieved through composition. It solves a complex problem by defining only those classes which are relevant to the problem and not involving the whole complex code into play.
14) Explain what a method is?
A method will affect only a particular object to which it is specified. Methods are verbs meaning they define actions which a particular object will perform. It also defines various other characteristics of a particular object.
15) Name the different Creational patterns in OO design?
There are three patterns of design out of which Creational patterns play an important role the various patterns described underneath this are: -
a) Factory pattern
b) Single ton pattern
c) Prototype pattern
d) Abstract factory pattern
e) Builder pattern
16) Explain about realistic modeling?
As we live in a world of objects, it logically follows that the object oriented approach models the real world accurately. The object oriented approach allows you to identify entities as objects having attributes and behavior.
17) Explain about the analysis phase?
The anlaysis or the object oriented analysis phase considers the system as a solution to a problem in its environment or domain. Developer concentrates on obtaining as much information as possible about the problem. Critical requirements needs to be identified.