oops concept in php

7
OOPS CONCEPT IN PHP What is meant by oops? OOP is a design philosophy. It stands for Object Oriented Programming. Object- Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable "objects". In order to clearly understand the object orientation, let’s take your hand” as an example. The “hand” is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled/ managed by a set of electrical signals sent through your shoulders (through an interface). So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it. Features of OOPS: OOP or Object Oriented Programming PHP has so many features like: Class Object Polymorphism Dynamic Binding...etc. What is a Class? A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Example: Class hand{ function left ($datal){ } function right ($datar){ } } $left hand=new hand(); // left hand object is created for the class hand $left hand-> left ($parma); $right hand=new hand();//right hand object is created for the class hand

Upload: selva-balaji

Post on 22-May-2015

828 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Oops concept in php

OOPS CONCEPT IN PHP

What is meant by oops?

OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable "objects".

In order to clearly understand the object orientation, let’s take your “hand” as an example. The “hand” is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled/ managed by a set of electrical signals sent through your shoulders (through an interface). So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it.

Features of OOPS:

OOP or Object Oriented Programming PHP has so many features like:

Class Object Polymorphism Dynamic Binding...etc.

What is a Class?

A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations. The class contains the methods and properties

Methods=function

Properties=variable

Example:

Class hand{ function left ($datal){ } function right ($datar){ } } $left hand=new hand(); // left hand object is created for the class hand $left hand-> left ($parma); $right hand=new hand();//right hand object is created for the class hand $left hand-> right ($parma);

Example: class Student // creation of class

{

}$ name=new Student();

Page 2: Oops concept in php

Here $name is the name of the object Student is the class name. For example Fruit is a class, where apple, orange are the object of this class. More than one object can be built from the same class at the same time, each one

independent of the others. The variable $this is a special variable and it refers to the same object ie. itself.

What is an Object?

An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior.

For example, the hand can grip something or a Student (object) can give the name or address.

In pure OOP terms an object is an instance of a class.

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.

Example:

<?php

class Books{

/* Member variables */ var $price; var $title; /* Member functions */ function setPrice($par){ $this->price = $var; } function getPrice(){ echo $this->price ."<br/>"; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title ." <br/>"; }}?>

Example: $physics = new Books; // object creation … $maths = new Books; $chemistry = new Books;

Page 3: Oops concept in php

Calling Member Functions:

After creating the objects, we will be able to call member functions related to that object. One member function will be able to process member variable of related object only.

To get the values from the functions:

Constructor

All objects can have a special built-in method called a 'constructor'. Constructors allow you to initialize your object's properties (translation: give your properties values,) when you instantiate (create) an object.

Note: If you create a __construct() function (it is your choice,) PHP will automatically call the __construct() method/function when you create an object from your class.

The 'construct' method starts with two underscores (__) and the word 'construct'.

We can call parent's constructor method using parent::__construct() from the child constructor.

Example: $physics->setTitle( "Physics for High School" ); // Function Calling by sending the values $chemistry->setTitle( "Advanced Chemistry" ); $maths->setTitle( "Algebra" ); $physics->setPrice( 10 ); $chemistry->setPrice( 15 ); $maths->setPrice( 7 );

Example: $physics->getTitle(); // Getting the values from the function .. $chemistry->getTitle(); $maths->getTitle(); $physics->getPrice(); $chemistry->getPrice(); $maths->getPrice();

Example: <?php class person { var $name;// variable or properties declare

function __construct($persons_name) { // Constructor creation for the class person $this->name = $persons_name; } function set_name($new_name) { $this->name = $new_name; } function get_name() {

Page 4: Oops concept in php

Create an object with a constructor

Now that we've created a constructor method, we can provide a value for the $name property when we create our person objects.

You 'feed' the constructor method by providing a list of arguments (like you do with a function) after the class name.

This is just a tiny example of how the mechanisms built into OO PHP can save you time and reduce the amount of code you need to write. Less code means less bugs.

Destructor

Constructors are very useful, as I am sure you will agree, but there is more: PHP also allows you to define class destructors - a function to be called when an object is deleted. PHP calls destructors as soon as objects are no longer available, and the destructor function, __destruct(), takes no parameters.

Example: <?php class person { var $name;// variable or properties declare

function __construct($persons_name) { // Constructor creation for the class person $this->name = $persons_name; } function set_name($new_name) { $this->name = $new_name; } function get_name() {

Example:$stefan = new person("Stefan Mischook");

This saves us from having to call the set_name() method reducing the amount of code. Constructors are common and are used often in PHP, Java etc.

<?php include("class_lib.php"); ?> </head> <body> <?php $stefan = new person("Stefan Mischook"); echo "Stefan's full name: ".$stefan->get_name(); ?> </body></html>

Example: <?php class Marko{ public function __construct($x){ echo "Not so fast, get some coffee $x !"; } public function __destruct() { echo "Destruction complete. Thank you. And good bye!"; }

Page 5: Oops concept in php

/*Not so fast, get some coffee (don't forget to mix it with some sugar and milk too) !Destruction complete. Thank you. And good bye!*/

Like constructors, destructors are only called once - you need to use parent::__destruct(). The key difference is that you should call parent::__destruct() after the local code for the destruction so that you are not destroying variables before using it, for example:

Access Specifiers Access Specifiers are the keywords in PHP, which will tell us about the visiblity of

property or method. Public, Private and Protected are the three Access Specifier Keywords in PHP. Below

is the difference between each of them. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by

inherited and parent classes. Members declared as private may only be accessed by the class that defines the

member. Default access specifier is Public.

Example: <?php class Marko{ public function __construct($x){ echo "Not so fast, get some coffee $x !"; } public function __destruct() { echo "Destruction complete. Thank you. And good bye!"; }

Example: public function __destruct() {    print "{$this->Name} is no more...\n";    parent::__destruct();

}

Example: <?php class person { var $name; public $height; protected $social_insurance; private $pinn_number; function __construct($persons_name) { $this->name = $persons_name; } function set_name($new_name) { $this->name = $new_name; }

function get_name() { return $this->name; } } $stefan = new person("Stefan Mischook");  

Page 6: Oops concept in php

Note: If you try to access a private property/variable outside of the class, you will get

this: 'Fatal error: Cannot access private property person::$pinn_numberin ...'

Inheritance

Inheritance is a fundamental capability/construct in OOP where you can use one class, as the base/basis for another class … or many other classes.

Why do it?

Doing this allows you to efficiently reuse the code found in your base class.

Say, you wanted to create a new 'employee' class … since we can say that 'employee' is a type/kind of 'person', they will share common properties and methods.

… Making some sense?

In this type of situation, inheritance can make your code lighter … because you are reusing the same code in two different classes. But unlike 'old-school' PHP:

1. You only have to type the code out once. 2. The actual code being reused, can be reused in many (unlimited) classes but it is

only typed out in one place … conceptually, this is sort-of like PHP includes().

Example: <?php class person { var $name; public $height; protected $social_insurance; private $pinn_number; function __construct($persons_name) { $this->name = $persons_name; } function set_name($new_name) { $this->name = $new_name; }

function get_name() { return $this->name; } } $stefan = new person("Stefan Mischook");  

// 'extends' is the keyword that enables inheritanceclass employee extends person { function __construct($employee_name) { $this->set_name($employee_name);