SlideShare une entreprise Scribd logo
1  sur  31
Télécharger pour lire hors ligne
PHP: Hypertext
Preprocessor
PHP - OOP
@d_danailov
PHP : Intro
Dimitar Danailov
Senior Developer at 158ltd.com
dimityr.danailov[at]gmail.com
Github
Slide Share
Founder at VarnaIT
Senior Developer at 158ltd.com
Topics Today
●
●
●
●
●
●

Object-oriented programming Overview
Objects & Classes
Abstraction and encapsulation
Inheritance
Polymorphism
Class Diagrams
OOP
Object-oriented programming (OOP) is a programming
paradigm that represents concepts as "objects" that have
data fields (attributes that describe the object) and
associated procedures known as methods. Objects, which
are usually instances of classes, are used to interact with
one another to design applications and computer
programs. C++, Objective-C, Smalltalk, Java and C# are
examples of object-oriented programming languages.
OOP (2)
●
●
●
●

Objects & Classes
Abstraction and encapsulation
Inheritance
Polymorphism
Objects & Classes
What is an object
Objects are the elements through which we perceive the
world around us. All objects have these characteristics :
● Identity
● State
● Behaviour
<?php
//Objects
$human = new stdClass();
$human->gender = 'm';
$human->age = 35;
$human->name = 'Todor Dimov';
$child1 = new stdClass();
$child1->name = 'Dimo Todorov';
$child2 = new stdClass();
$child2->name = 'Todorka Todorova';
$human->childrens = array($child1, $child2);
var_dump($human);
?>
Classes (classification of objects)
A class is a group of objects with same attributes and
behavior. The characteristics of a class are :
● A name
● Attributes
● Methods / Functions
<?php
/* Classes */
class Human {
private $name = null;
private $gender = null;
private $age = null;
private $childrens = array();
public function __construct($name, $gender, $age, $childrens
= array()) {
$this->name = $name;
$this->gender = $gender;
$this->age = $age;
$this->childrens = $childrens;
}
}
/* … */
?>
<?php
/* ... */
$child1 = new Human('Dimo Todorov', 'm', 12);
$child2 = new Human('Todorka Todorova', 'f', 16);
$childrens = array($child1, $child2);
$human = new Human('Todor Dimov', 'm', 35, $childrens);
var_dump($human);
?>
Abstraction and
encapsulation
Abstraction
In computer science, abstraction is the process by which
data and programs are defined with a representation
similar in form to its meaning (semantics), while hiding
away the implementation details. Abstraction tries to
reduce and factor out details so that the programmer can
focus on a few concepts at a time. A system can have
several abstraction layers whereby different meanings
and amounts of detail are exposed to the programmer.
Encapsulation
Encapsulation is the practice of including in an object
everything it needs hidden from the other objects in the
system.
Inheritance
Inheritance
In object-oriented programming (OOP), inheritance is a
way to establish Is-a relationships between objects. In
classical inheritance where objects are defined by
classes, classes can inherit attributes and behavior from
pre-existing classes called base classes, superclasses, or
parent classes.
// Inheritance
class Human
{
private $name = null;
private $gender = null;
private $age = null;
public function __construct($name, $gender, $age) {
$this->name = $name;
$this->gender = $gender;
$this->age = $age;
}
static function prinHello($name) {
echo 'Hello, ' . $name;
}
private function greetings() {
echo 'Greetings';
}
}
class ParentClass extends Human {
private $name = null;
private $gender = null;
private $age = null;
private $childrens = array();
public function __construct ($name, $gender, $age) {
$this->name = $name;
$this->gender = $gender;
$this->age = $age;
}
public function getName() {
//$this->greetings();
return parent::prinHello($this->name);
}
public function getChildrens () {
return $this->childrens;
}
public function setChildren (Child $child) {
$this->childrens[] = $child;
}
}
Class Child extends Human {
private $parents = array();
public function __construct($name, $gender, $age) {
$this->name = $name;
$this->gender = $gender;
$this->age = $age;
}
public function getParents() {
return $this->parents;
}
public function setParent(ParentClass $parent) {
$this->parents[] = $parent;
}
}
Polymorphism
Polymorphism
Polymorphism describes a pattern in object oriented
programming in which classes have different functionality
while sharing a common interface.
Abstract Classes
Class Abstraction
PHP 5 introduces abstract classes and methods. Classes
defined as abstract may not be instantiated, and any class
that contains at least one abstract method must also be
abstract. Methods defined as abstract simply declare the
method's signature - they cannot define the
implementation.
<?php
abstract class AbstractHuman {
private $name = null;
private $gender = null;
private $age = null;
public function __construct ($name, $gender, $age) {
$this->name = $name;
$this->gender = $gender;
$this->age = $age;
}
abstract public function getName();
abstract public function setName($name);
abstract public function getGender ();
abstract public function setGender ($gender);
abstract public function getAge();
abstract public function setAge($age);
}
?>
<?php
class ParentClass extends AbstractHuman {
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getGender () {
return $this->gender;
}
public function setGender ($gender) {
$this->gender = $gender;
}

// ...
}
?>
Interfaces
Object Interfaces
Object interfaces allow you to create code which specifies
which methods a class must implement, without having to
define how these methods are handled.
Interfaces are defined using the interface keyword, in the
same way as a standard class, but without any of the
methods having their contents defined.
All methods declared in an interface must be public, this is
the nature of an interface.
<?php
// Interfaces
interface iHuman {
public function getName();
public function setName($name);
public function getGender();
public function setGender($gender);
public function getAge();
public function setAge($age);
}
?>
<?php
class ParentClass implements iHuman {
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getGender() {
return $this->gender;
}
public function setGender($gender) {
$this->gender = $gender;
}
// ...
}
?>
Class Diagrams
Questions ?
Dimitar Danailov
Senior Developer at 158ltd.com
dimityr.danailov[at]gmail.com
Github
Slide Share
Founder at VarnaIT
Senior Developer at 158ltd.com

Contenu connexe

Plus de Dimitar Danailov

Data Visualization and D3Js
Data Visualization and D3JsData Visualization and D3Js
Data Visualization and D3JsDimitar Danailov
 
#Productivity - {S:01 Ep:03}
#Productivity - {S:01 Ep:03} #Productivity - {S:01 Ep:03}
#Productivity - {S:01 Ep:03} Dimitar Danailov
 
#Productivity - {S:01 Ep:02}
#Productivity - {S:01 Ep:02}#Productivity - {S:01 Ep:02}
#Productivity - {S:01 Ep:02}Dimitar Danailov
 
Cloud Conf Varna - Cloud Application with AWS Lambda functions
Cloud Conf Varna - Cloud Application with AWS Lambda functionsCloud Conf Varna - Cloud Application with AWS Lambda functions
Cloud Conf Varna - Cloud Application with AWS Lambda functionsDimitar Danailov
 
DEV.BG - Angular 1 and Jasmine (Unit Testing and TDD)
DEV.BG - Angular 1 and Jasmine (Unit Testing and TDD)DEV.BG - Angular 1 and Jasmine (Unit Testing and TDD)
DEV.BG - Angular 1 and Jasmine (Unit Testing and TDD)Dimitar Danailov
 
Building modern Progressive Web Apps with Polymer
Building modern Progressive Web Apps with PolymerBuilding modern Progressive Web Apps with Polymer
Building modern Progressive Web Apps with PolymerDimitar Danailov
 
Typescript - MentorMate Academy
Typescript - MentorMate AcademyTypescript - MentorMate Academy
Typescript - MentorMate AcademyDimitar Danailov
 
HackConf2016 - Ruby on Rails: Unexpected journey
HackConf2016 - Ruby on Rails: Unexpected journeyHackConf2016 - Ruby on Rails: Unexpected journey
HackConf2016 - Ruby on Rails: Unexpected journeyDimitar Danailov
 
Microservices - Code Voyagers Sofia
Microservices - Code Voyagers SofiaMicroservices - Code Voyagers Sofia
Microservices - Code Voyagers SofiaDimitar Danailov
 
Mongo DB Terms - Mentormate Academy
Mongo DB Terms - Mentormate AcademyMongo DB Terms - Mentormate Academy
Mongo DB Terms - Mentormate AcademyDimitar Danailov
 
Startup Europe Week - Cloud Conf Varna & GDG Varna
Startup Europe Week - Cloud Conf Varna & GDG VarnaStartup Europe Week - Cloud Conf Varna & GDG Varna
Startup Europe Week - Cloud Conf Varna & GDG VarnaDimitar Danailov
 
MicroServices: Advantages ans Disadvantages
MicroServices: Advantages ans DisadvantagesMicroServices: Advantages ans Disadvantages
MicroServices: Advantages ans DisadvantagesDimitar Danailov
 
Softuni.bg - Microservices
Softuni.bg - MicroservicesSoftuni.bg - Microservices
Softuni.bg - MicroservicesDimitar Danailov
 
Cloud Conf Varna: Vagrant and Amazon
Cloud Conf Varna: Vagrant and AmazonCloud Conf Varna: Vagrant and Amazon
Cloud Conf Varna: Vagrant and AmazonDimitar Danailov
 
HackConf2015 - Ruby on Rails: Unexpected journey
HackConf2015 - Ruby on Rails: Unexpected journeyHackConf2015 - Ruby on Rails: Unexpected journey
HackConf2015 - Ruby on Rails: Unexpected journeyDimitar Danailov
 
Lighting talks - Microservices
Lighting talks - MicroservicesLighting talks - Microservices
Lighting talks - MicroservicesDimitar Danailov
 

Plus de Dimitar Danailov (20)

Data Visualization and D3Js
Data Visualization and D3JsData Visualization and D3Js
Data Visualization and D3Js
 
#Productivity - {S:01 Ep:03}
#Productivity - {S:01 Ep:03} #Productivity - {S:01 Ep:03}
#Productivity - {S:01 Ep:03}
 
#Productivity - {S:01 Ep:02}
#Productivity - {S:01 Ep:02}#Productivity - {S:01 Ep:02}
#Productivity - {S:01 Ep:02}
 
#Productivity s01 ep02
#Productivity s01 ep02#Productivity s01 ep02
#Productivity s01 ep02
 
#Productivity s01 ep01
#Productivity s01 ep01#Productivity s01 ep01
#Productivity s01 ep01
 
Cloud Conf Varna - Cloud Application with AWS Lambda functions
Cloud Conf Varna - Cloud Application with AWS Lambda functionsCloud Conf Varna - Cloud Application with AWS Lambda functions
Cloud Conf Varna - Cloud Application with AWS Lambda functions
 
DEV.BG - Angular 1 and Jasmine (Unit Testing and TDD)
DEV.BG - Angular 1 and Jasmine (Unit Testing and TDD)DEV.BG - Angular 1 and Jasmine (Unit Testing and TDD)
DEV.BG - Angular 1 and Jasmine (Unit Testing and TDD)
 
Building modern Progressive Web Apps with Polymer
Building modern Progressive Web Apps with PolymerBuilding modern Progressive Web Apps with Polymer
Building modern Progressive Web Apps with Polymer
 
Typescript - MentorMate Academy
Typescript - MentorMate AcademyTypescript - MentorMate Academy
Typescript - MentorMate Academy
 
HackConf2016 - Ruby on Rails: Unexpected journey
HackConf2016 - Ruby on Rails: Unexpected journeyHackConf2016 - Ruby on Rails: Unexpected journey
HackConf2016 - Ruby on Rails: Unexpected journey
 
Microservices - Code Voyagers Sofia
Microservices - Code Voyagers SofiaMicroservices - Code Voyagers Sofia
Microservices - Code Voyagers Sofia
 
Mongo DB Terms - Mentormate Academy
Mongo DB Terms - Mentormate AcademyMongo DB Terms - Mentormate Academy
Mongo DB Terms - Mentormate Academy
 
Startup Europe Week - Cloud Conf Varna & GDG Varna
Startup Europe Week - Cloud Conf Varna & GDG VarnaStartup Europe Week - Cloud Conf Varna & GDG Varna
Startup Europe Week - Cloud Conf Varna & GDG Varna
 
GDG Varna - Hadoop
GDG Varna - HadoopGDG Varna - Hadoop
GDG Varna - Hadoop
 
MicroServices: Advantages ans Disadvantages
MicroServices: Advantages ans DisadvantagesMicroServices: Advantages ans Disadvantages
MicroServices: Advantages ans Disadvantages
 
GDG Varna - EcmaScript 6
GDG Varna - EcmaScript 6GDG Varna - EcmaScript 6
GDG Varna - EcmaScript 6
 
Softuni.bg - Microservices
Softuni.bg - MicroservicesSoftuni.bg - Microservices
Softuni.bg - Microservices
 
Cloud Conf Varna: Vagrant and Amazon
Cloud Conf Varna: Vagrant and AmazonCloud Conf Varna: Vagrant and Amazon
Cloud Conf Varna: Vagrant and Amazon
 
HackConf2015 - Ruby on Rails: Unexpected journey
HackConf2015 - Ruby on Rails: Unexpected journeyHackConf2015 - Ruby on Rails: Unexpected journey
HackConf2015 - Ruby on Rails: Unexpected journey
 
Lighting talks - Microservices
Lighting talks - MicroservicesLighting talks - Microservices
Lighting talks - Microservices
 

Dernier

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 

Dernier (20)

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 

VFU SEM - PHP OOP [12.10.2013]

  • 2. PHP : Intro Dimitar Danailov Senior Developer at 158ltd.com dimityr.danailov[at]gmail.com Github Slide Share Founder at VarnaIT Senior Developer at 158ltd.com
  • 3. Topics Today ● ● ● ● ● ● Object-oriented programming Overview Objects & Classes Abstraction and encapsulation Inheritance Polymorphism Class Diagrams
  • 4. OOP Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs. C++, Objective-C, Smalltalk, Java and C# are examples of object-oriented programming languages.
  • 5. OOP (2) ● ● ● ● Objects & Classes Abstraction and encapsulation Inheritance Polymorphism
  • 7. What is an object Objects are the elements through which we perceive the world around us. All objects have these characteristics : ● Identity ● State ● Behaviour
  • 8. <?php //Objects $human = new stdClass(); $human->gender = 'm'; $human->age = 35; $human->name = 'Todor Dimov'; $child1 = new stdClass(); $child1->name = 'Dimo Todorov'; $child2 = new stdClass(); $child2->name = 'Todorka Todorova'; $human->childrens = array($child1, $child2); var_dump($human); ?>
  • 9. Classes (classification of objects) A class is a group of objects with same attributes and behavior. The characteristics of a class are : ● A name ● Attributes ● Methods / Functions
  • 10. <?php /* Classes */ class Human { private $name = null; private $gender = null; private $age = null; private $childrens = array(); public function __construct($name, $gender, $age, $childrens = array()) { $this->name = $name; $this->gender = $gender; $this->age = $age; $this->childrens = $childrens; } } /* … */ ?>
  • 11. <?php /* ... */ $child1 = new Human('Dimo Todorov', 'm', 12); $child2 = new Human('Todorka Todorova', 'f', 16); $childrens = array($child1, $child2); $human = new Human('Todor Dimov', 'm', 35, $childrens); var_dump($human); ?>
  • 13. Abstraction In computer science, abstraction is the process by which data and programs are defined with a representation similar in form to its meaning (semantics), while hiding away the implementation details. Abstraction tries to reduce and factor out details so that the programmer can focus on a few concepts at a time. A system can have several abstraction layers whereby different meanings and amounts of detail are exposed to the programmer.
  • 14. Encapsulation Encapsulation is the practice of including in an object everything it needs hidden from the other objects in the system.
  • 16. Inheritance In object-oriented programming (OOP), inheritance is a way to establish Is-a relationships between objects. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses, or parent classes.
  • 17. // Inheritance class Human { private $name = null; private $gender = null; private $age = null; public function __construct($name, $gender, $age) { $this->name = $name; $this->gender = $gender; $this->age = $age; } static function prinHello($name) { echo 'Hello, ' . $name; } private function greetings() { echo 'Greetings'; } }
  • 18. class ParentClass extends Human { private $name = null; private $gender = null; private $age = null; private $childrens = array(); public function __construct ($name, $gender, $age) { $this->name = $name; $this->gender = $gender; $this->age = $age; } public function getName() { //$this->greetings(); return parent::prinHello($this->name); } public function getChildrens () { return $this->childrens; } public function setChildren (Child $child) { $this->childrens[] = $child; } }
  • 19. Class Child extends Human { private $parents = array(); public function __construct($name, $gender, $age) { $this->name = $name; $this->gender = $gender; $this->age = $age; } public function getParents() { return $this->parents; } public function setParent(ParentClass $parent) { $this->parents[] = $parent; } }
  • 21. Polymorphism Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface.
  • 23. Class Abstraction PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation.
  • 24. <?php abstract class AbstractHuman { private $name = null; private $gender = null; private $age = null; public function __construct ($name, $gender, $age) { $this->name = $name; $this->gender = $gender; $this->age = $age; } abstract public function getName(); abstract public function setName($name); abstract public function getGender (); abstract public function setGender ($gender); abstract public function getAge(); abstract public function setAge($age); } ?>
  • 25. <?php class ParentClass extends AbstractHuman { public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getGender () { return $this->gender; } public function setGender ($gender) { $this->gender = $gender; } // ... } ?>
  • 27. Object Interfaces Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled. Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined. All methods declared in an interface must be public, this is the nature of an interface.
  • 28. <?php // Interfaces interface iHuman { public function getName(); public function setName($name); public function getGender(); public function setGender($gender); public function getAge(); public function setAge($age); } ?>
  • 29. <?php class ParentClass implements iHuman { public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getGender() { return $this->gender; } public function setGender($gender) { $this->gender = $gender; } // ... } ?>
  • 31. Questions ? Dimitar Danailov Senior Developer at 158ltd.com dimityr.danailov[at]gmail.com Github Slide Share Founder at VarnaIT Senior Developer at 158ltd.com