oop in php
TRANSCRIPT
Object Oriented Programming in PHP
These slides
Object Class
Visibility
Encapsulation
Polymorphism
Inheritance
Abstraction
Final
Static
Constructor
Destructor
Interface
Pattern
What is OOP?
It is a method of programming based on a hierarchy of classes, and well-defined and cooperating objects.
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
Why OOP?
Modularization
Abstraction – Understandability
Encapsulation -- Information Hiding
Composability -- Structured Design
Hierarchy
Continuity
Why OOP?
Modularization
Decompose problem into smaller sub-problems that can be solved separately
Why OOP?
Abstraction -- Understandability
Terminology of the problem domain is reflected in the software solution. Individual modules are understandable by human readers
Why OOP?
Encapsulation -- Information Hiding
Hide complexity from the user of a software. Protect low-level functionality.
Why OOP?
Composability -- Structured Design
Interfaces allow to freely combine modules to produce new systems.
Why OOP?
Hierarchy
Incremental development from small and simple to more complex modules.
Why OOP?
Continuity
Changes and maintenance in only a few modules does not affect the architecture.
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)
Object in real world
Object
Object has own properties and actions
Object can communicate with other object
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
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 :
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
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
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
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
Object Assignment in PHP
$emran = new Person(‘Emran Hasan’); $myfriend = $emran;
$emran->setName( ‘Md Emran Hasan’);
echo $myfriend ->getName(); //Output : Md Emran Hasan
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.
Simple Class Inheritance
class Student extends Person {
}
$newStudent = new Student (‘Hasin Hyder’);
echo $newStudent->getName(); //Output will be : Hasin Hyder
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
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
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
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
Abstract <?php abstract class Weapon { private $serialNumber; abstract public function fire();
public function __construct($serialNumber) { $this->serialNumber = $serialNumber; }
public function getSerialNumber() { return $this->serialNumber; } } ?>
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();
Interface
interface Crud { public function get() {} public function insert() {} public function update() { } public function delete() { } }
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 } }
More on OOP
Polymorphism
Coupling
Design Patterns
Reference
Everything about PHP http://php.net
Object Oriented Programming
http://www.oop.esmartkid.com/index.htm
These slides
http://www.apueee.com