oop in php

37

Click here to load reader

Upload: tarek-mahmud-apu

Post on 27-May-2015

780 views

Category:

Education


2 download

TRANSCRIPT

Page 1: OOP in PHP

Object Oriented Programming in PHP

Page 2: OOP in PHP
Page 3: OOP in PHP

These slides

Page 4: OOP in PHP
Page 5: OOP in PHP

Object Class

Visibility

Encapsulation

Polymorphism

Inheritance

Abstraction

Final

Static

Constructor

Destructor

Interface

Pattern

Page 6: OOP in PHP

What is OOP?

It is a method of programming based on a hierarchy of classes, and well-defined and cooperating objects.

Page 7: OOP in PHP

Why OOP?

  Offers powerful model for writing computer software.

  Easy to partition the work in a project based on objects.

  Object-oriented systems can be easily upgraded from small to large scale.

  Reduces software maintenance and developing costs.

  Accept the changes in user requirements or later developments.

  help to develop high quality software easily

Page 8: OOP in PHP

Why OOP?

  Modularization

  Abstraction – Understandability

  Encapsulation -- Information Hiding

  Composability -- Structured Design

  Hierarchy

  Continuity

Page 9: OOP in PHP

Why OOP?

Modularization

Decompose problem into smaller sub-problems that can be solved separately

Page 10: OOP in PHP

Why OOP?

Abstraction -- Understandability

Terminology of the problem domain is reflected in the software solution. Individual modules are understandable by human readers

Page 11: OOP in PHP

Why OOP?

Encapsulation -- Information Hiding

Hide complexity from the user of a software. Protect low-level functionality.

Page 12: OOP in PHP

Why OOP?

Composability -- Structured Design

Interfaces allow to freely combine modules to produce new systems.

Page 13: OOP in PHP

Why OOP?

Hierarchy

Incremental development from small and simple to more complex modules.

Page 14: OOP in PHP

Why OOP?

Continuity

Changes and maintenance in only a few modules does not affect the architecture.

Page 15: OOP in PHP

Main OOP Language Features

  Classes (Modularization, structure)   Inheritance / extends (Hierarchy of modules, incremental

development   Encapsulation ( Public , Private, Protected)   Composability ( Interfaces / Abstraction )   Polymorphism ( Hierarchy of modules, incremental development)

Page 16: OOP in PHP

Object in real world

Page 17: OOP in PHP

Object

  Object has own properties and actions

  Object can communicate with other object

Page 18: OOP in PHP

OOP Features in PHP   Class Constants   Autoloading   Constructor and Destructors   Visibility   Inheritance   Static keyword   Class Abstraction   Object Interfaces   Overloading   Object Iteration   Magic Methods  Object Cloning   Type Hinting   Object Serialization

Page 19: OOP in PHP

Declaration of Class

class Person {

var $name = ‘default name’; function Person()

{ //constructor }

}

PHP 4 :

class Person {

public $name = ‘default name’; //automatically calling __construct() magic function }

PHP 5 :

Page 20: OOP in PHP

Declaration of Class

class Person {

var $name = ‘default name’; function Person()

{ //constructor }

}

PHP 4 :

class Person {

public $name = ‘default name’; //automatically calling __construct() magic function }

PHP 5 :

  Should not a PHP reserved word

  Starts with a letter or underscore, followed by any number of letters, numbers, or underscores

Page 21: OOP in PHP

Object in PHP

class Person { public $name;

function setName ($personName) { $this->name = $personName; } function getName ( ) { return $this->name; }

} //Creating A Object $emran = new Person(); $emran->setName(‘Emran Hasan’);

echo $emran->getName(); //Output : Emran Hasan

<?php

Page 22: OOP in PHP

Class   Class is a description of an object   A user-defined data type that contains the variables, properties

and methods in it.   Class never executes   It’s possible to create unlimited no of instance from a class.

Object   Object is an instance of a class.   It’s an executable copy of a class

Page 23: OOP in PHP

Using __construct magic function

class Person { public $name ;

function __construct ($personName) { $this->name = $personName; } function getName ( ) { return $this->name; }

} $emran = new Person(‘Emran Hasan’); $alamgir = new Person(‘Alamgir Hossain’);

echo $emran->getName(); // will print Emran Hasan echo $alamgir ->getName(); // will print Alamgir Hossain

Page 24: OOP in PHP

Object Assignment in PHP

$emran = new Person(‘Emran Hasan’); $myfriend = $emran;

$emran->setName( ‘Md Emran Hasan’);

echo $myfriend ->getName(); //Output : Md Emran Hasan

Page 25: OOP in PHP

Inheritance

  It’s an object-oriented concept that helps objects to work.

  It defines relationships among classes

  Inheritance means that the language gives the ability to extend or enhance existing objects.

Page 26: OOP in PHP

Simple Class Inheritance

class Student extends Person {

}

$newStudent = new Student (‘Hasin Hyder’);

echo $newStudent->getName(); //Output will be : Hasin Hyder

Page 27: OOP in PHP

Simple Class Inheritance class Student extends Person {

protected $roll; function setRoll( $rollNo) { $this->roll = $rollNo; } function getRoll( ) { return $this->roll; }

} $newStudent = new Student (‘Hasin Hyder’); $newStudent ->setRoll( ‘98023’);

echo $newStudent->getName(); echo “Roll no : ” . $newStudent->getRoll () ; //Output will be : Hasin Hyder , Roll no : 98023

Page 28: OOP in PHP

Protecting Data With Visibility

  Public (default) - the variable can be accessed and changed globally by anything.

  Protected - the variable can be accessed and changed only by direct descendants (those who inherit it using the extends statement) of the class and class its self

  Private - the variable can be accessed and changed only from within the class

Page 29: OOP in PHP

Visibility class MyClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private';

function printHello() { echo $this->public; echo $this->protected; echo $this->private; } }

$obj = new MyClass(); echo $obj->public; // Public echo $obj->protected; // Fatal Error echo $obj->private; // Fatal Error $obj->printHello(); // Shows Public, Protected and Private

Page 30: OOP in PHP

Visibility class MyClass2 extends MyClass { // We can redeclare the public and protected method, but not private protected $protected = 'Protected2';

function printHello() { echo $this->public; echo $this->protected; echo $this->private; } }

$obj2 = new MyClass2(); echo $obj2->public; // Works echo $obj2->private; // Undefined echo $obj2->protected; // Fatal Error $obj2->printHello(); // Shows Public, Protected2, Undefined

Page 31: OOP in PHP

Abstract <?php abstract class Weapon { private $serialNumber; abstract public function fire();

public function __construct($serialNumber) { $this->serialNumber = $serialNumber; }

public function getSerialNumber() { return $this->serialNumber; } } ?>

Page 32: OOP in PHP

Abstraction <?php

class Gun extends Weapon { public function fire() { echo “gun fired”; } }

class Cannon extends Weapon { public function fire() { echo “canon fired”; } }

?>

$newGun = new Gun(‘AK-47’); $newGun->fire(); $newGun->getSerialNumber();

$canon = new Canon(‘CAN-5466’); $canon->fire(); $canon->getSerialNumber();

Page 33: OOP in PHP

Interface

interface Crud { public function get() {} public function insert() {} public function update() { } public function delete() { } }

Page 34: OOP in PHP

Interface

Class Product implements Crud { public function get() {

//here goes the code } public function insert() { //here goes the code } public function update() { //here goes the code } public function delete() { //here goes the code } }

Class Customer implements Crud public function get() {

//here goes the code } public function insert() { //here goes the code } public function update() { //here goes the code } public function delete() { //here goes the code } }

Page 35: OOP in PHP

More on OOP

  Polymorphism

  Coupling

  Design Patterns

Page 36: OOP in PHP

Questions?

Thanks

Tarek Mahmud Apu Founder, Wneeds [email protected]

Page 37: OOP in PHP

Reference

  Everything about PHP http://php.net

  Object Oriented Programming

http://www.oop.esmartkid.com/index.htm

  These slides

http://www.apueee.com