Download - OOP in PHP
![Page 1: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/1.jpg)
Object Oriented Programming in PHP
![Page 2: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/2.jpg)
![Page 3: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/3.jpg)
These slides
![Page 4: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/4.jpg)
![Page 5: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/5.jpg)
Object Class
Visibility
Encapsulation
Polymorphism
Inheritance
Abstraction
Final
Static
Constructor
Destructor
Interface
Pattern
![Page 6: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/6.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/7.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/8.jpg)
Why OOP?
Modularization
Abstraction – Understandability
Encapsulation -- Information Hiding
Composability -- Structured Design
Hierarchy
Continuity
![Page 9: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/9.jpg)
Why OOP?
Modularization
Decompose problem into smaller sub-problems that can be solved separately
![Page 10: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/10.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/11.jpg)
Why OOP?
Encapsulation -- Information Hiding
Hide complexity from the user of a software. Protect low-level functionality.
![Page 12: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/12.jpg)
Why OOP?
Composability -- Structured Design
Interfaces allow to freely combine modules to produce new systems.
![Page 13: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/13.jpg)
Why OOP?
Hierarchy
Incremental development from small and simple to more complex modules.
![Page 14: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/14.jpg)
Why OOP?
Continuity
Changes and maintenance in only a few modules does not affect the architecture.
![Page 15: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/15.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/16.jpg)
Object in real world
![Page 17: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/17.jpg)
Object
Object has own properties and actions
Object can communicate with other object
![Page 18: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/18.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/19.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/20.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/21.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/22.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/23.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/24.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/25.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/26.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/27.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/28.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/29.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/30.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/31.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/32.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/33.jpg)
Interface
interface Crud { public function get() {} public function insert() {} public function update() { } public function delete() { } }
![Page 34: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/34.jpg)
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](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/35.jpg)
More on OOP
Polymorphism
Coupling
Design Patterns
![Page 37: OOP in PHP](https://reader037.vdocument.in/reader037/viewer/2022100304/556591fcd8b42a2b6d8b51d6/html5/thumbnails/37.jpg)
Reference
Everything about PHP http://php.net
Object Oriented Programming
http://www.oop.esmartkid.com/index.htm
These slides
http://www.apueee.com