SlideShare a Scribd company logo
1 of 35
Download to read offline
DEMYSTIFYING OBJECT-ORIENTED
PROGRAMMING
MAKING SENSE OF ALL THE MUMBO JUMBO
HTTPS://GITHUB.COM/SKETCHINGS/OOP-BASICS
TERMINOLOGY
THE SINGLE MOST IMPORTANT PART
TERMS
• Class (properties, methods)
• Object
• Instance
• Abstraction
• Encapsulation
TERMS CONTINUED
• Polymorphism
• Inheritance
• Interface
• Abstract Class
• Traits
• Namespaces
CLASS
• A template/blueprint that facilitates creation of objects.
A set of program statements to do a certain task. Usually
represents a noun, such as a person, place or thing.
• Includes properties and methods — which are class
functions
OBJECT
• Instance of a class.
• In the real world object is a material thing that
can be seen and touched.
• In OOP, object is a self-contained entity that
consists of both data and procedures.
INSTANCE
• Single occurrence/copy of an object
• There might be one or several objects, but an
instance is a specific copy, to which you can have
a reference
class User { //class

private $name; //property

public getName() { //method

echo $this->name;

}

}
$user1 = new User(); //first instance of object
$user2 = new User(); //second instance of object
ABSTRACTION
• “An abstraction denotes the essential characteristics
of an object that distinguish it from all other kinds of
object and thus provide crisply defined conceptual
boundaries, relative to the perspective of the
viewer.”

— G. Booch
• This is the class architecture itself.
• Use something without knowing exactly how it works
ENCAPSULATION
• Scope. Controls who can access what. Restricting
access to some of the object’s components (properties
and methods), preventing unauthorized access.
• Public - everyone
• Protected - inherited classes
• Private - class itself, not children
class User {

protected $name;

protected $title;

public function getFormattedSalutation() {

return $this->getSalutation();

}

protected function getSalutation() {

return $this->title . " " . $this->name;

}

public function getName() {

return $this->name;

}

public function setName($name) {

$this->name = $name;

}

public function getTitle() {

return $this->title;

}

public function setTitle($title) {

$this->title = $title;

}

}
CREATING / USING THE OBJECT INSTANCE
$user = new User();

$user->setName("Jane Smith");

$user->setTitle("Ms");

echo $user->getFormattedSalutation();
When the script is run, it will return:
Ms Jane Smith
CONSTRUCTOR METHOD & MAGIC METHODS
class User {

protected $name;

protected $title;



public function __construct($name, $title) {

$this->name = $name;

$this->title = $title;

}



public function __toString() {

return $this->getFormattedSalutation();

}

...

}
For more see http://php.net/manual/en/language.oop5.magic.php
CREATING / USING THE OBJECT INSTANCE
$user = new User("Jane Smith","Ms");

echo $user;
When the script is run, it will return the same result as before:
Ms Jane Smith
POLYMORPHISM
DRY and Sharing Code
INHERITANCE: PASSES KNOWLEDGE DOWN
• Subclass, parent and a child relationship, allows for
reusability, extensibility.
• Additional code to an existing class without modifying it.
Uses keyword “extends”
• NUTSHELL: create a new class based on an existing class
with more data, create new objects based on this class
CREATING AND USING A CHILD CLASS
class Developer extends User {

public $skills = array();

public function getSalutation() {

return $this->title . " " . $this->name. ", Developer";

}

public function getSkillsString() {

echo implode(", ",$this->skills);

}

}
$developer = new Developer("Jane Smith", "Ms");

echo $developer;

echo "<br />";

$developer->skills = array("JavasScript", "HTML", "CSS");

$developer->skills[] = "PHP";

$developer->getSkillsString();
WHEN RUN, THE SCRIPT RETURNS:
Ms Jane Smith, Developer
JavasScript, HTML, CSS, PHP
INTERFACE
• Interface, specifies which methods a class must implement.
• All methods in interface must be public.
• Multiple interfaces can be implemented by using comma
separation
• Interface may contain a CONSTANT, but may not be
overridden by implementing class
interface UserInterface {
public function getFormattedSalutation();
public function getName();
public function setName($name);
public function getTitle();
public function setTitle($title);
}
class User implements UserInterface { … }
ABSTRACT CLASS
An abstract class is a mix between an interface and a class. It can define
functionality as well as interface.
Classes extending an abstract class must implement all of the abstract
methods defined in the abstract class.
abstract class User { //class
public $name; //property
public getName() { //method
echo $this->name;
}
abstract public function setName($name);
}
class Developer extends User { … }
COMPOSITION
Horizontal Code Reuse
TRAITS
• Multiple traits can be implemented
CREATING TRAITS
trait Toolkit {

public $tools = array();

public function setTools($task) {

switch {

case “eat":

$this->tools[] = ("Spoon", "Fork", "Knife");

exit;

...

}

}

public function showTools() {

echo implode(", ",$this->skills);

}

}
USING TRAITS
class Developer extends User {

use Toolkit;



...

}
$developer = new Developer("Jane Smith", "Ms");

echo $developer;

echo "<br />";

$developer->setTools("Eat");

echo $developer->showTools();
WHEN RUN, THE SCRIPT RETURNS:
Ms Jane Smith
Spoon, Fork, Knife
NAMESPACES
Prevent Code Collision
NAMESPACES
• Help create a new layer of code encapsulation
• Keep properties from colliding between areas of your code
• Only classes, interfaces, functions and constants are
affected
• Anything that does not have a namespace is considered in
the Global namespace (namespace = "")
NAMESPACES
• Must be declared first (except 'declare)
• Can define multiple in the same file
• You can define that something be used in the "Global" namespace by
enclosing a non-labeled namespace in {} brackets.
• Use namespaces from within other namespaces, along with aliasing
• namespace myUser;
• class User { //class
• public $name; //property
• public getName() { //method
• echo $this->name;
• }
• public function setName($name);
• }
• class Developer extends myUserUser { … }
EXPLANATION COMPLETE
QUESTION AND ANSWER TIME
ALENA HOLLIGAN
@sketchings

sketchings.com
Wife and Mother of 3 young children 

PHP Teacher at Treehouse

PHP Users Group Leader

Women Who Code Leader
https://joind.in/talk/6b753
CHALLENGES
1. Change to User class to an abstract class.
2. Throw an error because your access is too restricted.
3. Extend the User class for another type of user, such as our
Developer example
4. Define 2 “User” classes in one file using namespacing
https://github.com/sketchings/oop-basics
RESOURCES
• LeanPub: The Essentials of Object Oriented PHP
• Head First Object-Oriented Analysis and Design

More Related Content

What's hot

How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrowPete McFarlane
 
GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2Heather Rock
 
Introduction to Twig
Introduction to TwigIntroduction to Twig
Introduction to Twigmarkstory
 
Revisiting SOLID Principles
Revisiting  SOLID Principles Revisiting  SOLID Principles
Revisiting SOLID Principles Anis Ahmad
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretssmueller_sandsmedia
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency InjectionRifat Nabi
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHPAhmed Swilam
 
Gérer vos objets
Gérer vos objetsGérer vos objets
Gérer vos objetsThomas Gasc
 
Writing Sensible Code
Writing Sensible CodeWriting Sensible Code
Writing Sensible CodeAnis Ahmad
 
PHP object calisthenics
PHP object calisthenicsPHP object calisthenics
PHP object calisthenicsGiorgio Cefaro
 
Jooctrine - Doctrine ORM in Joomla!
Jooctrine - Doctrine ORM in Joomla!Jooctrine - Doctrine ORM in Joomla!
Jooctrine - Doctrine ORM in Joomla!Herman Peeren
 
Building Large jQuery Applications
Building Large jQuery ApplicationsBuilding Large jQuery Applications
Building Large jQuery ApplicationsRebecca Murphey
 

What's hot (19)

How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
Migrare da symfony 1 a Symfony2
 Migrare da symfony 1 a Symfony2  Migrare da symfony 1 a Symfony2
Migrare da symfony 1 a Symfony2
 
GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2
 
Web 9 | OOP in PHP
Web 9 | OOP in PHPWeb 9 | OOP in PHP
Web 9 | OOP in PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Introduction to Twig
Introduction to TwigIntroduction to Twig
Introduction to Twig
 
Revisiting SOLID Principles
Revisiting  SOLID Principles Revisiting  SOLID Principles
Revisiting SOLID Principles
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Class 2 - Introduction to PHP
Class 2 - Introduction to PHPClass 2 - Introduction to PHP
Class 2 - Introduction to PHP
 
Gérer vos objets
Gérer vos objetsGérer vos objets
Gérer vos objets
 
Writing Sensible Code
Writing Sensible CodeWriting Sensible Code
Writing Sensible Code
 
PHP object calisthenics
PHP object calisthenicsPHP object calisthenics
PHP object calisthenics
 
PHP pod mikroskopom
PHP pod mikroskopomPHP pod mikroskopom
PHP pod mikroskopom
 
Web 11 | AJAX + JSON + PHP
Web 11 | AJAX + JSON + PHPWeb 11 | AJAX + JSON + PHP
Web 11 | AJAX + JSON + PHP
 
tutorial7
tutorial7tutorial7
tutorial7
 
Web 10 | PHP with MySQL
Web 10 | PHP with MySQLWeb 10 | PHP with MySQL
Web 10 | PHP with MySQL
 
Jooctrine - Doctrine ORM in Joomla!
Jooctrine - Doctrine ORM in Joomla!Jooctrine - Doctrine ORM in Joomla!
Jooctrine - Doctrine ORM in Joomla!
 
Building Large jQuery Applications
Building Large jQuery ApplicationsBuilding Large jQuery Applications
Building Large jQuery Applications
 

Similar to Demystifying Object-Oriented Programming - Midwest PHP

Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Alena Holligan
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Alena Holligan
 
Take the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpTake the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpAlena Holligan
 
Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017Alena Holligan
 
Obect-Oriented Collaboration
Obect-Oriented CollaborationObect-Oriented Collaboration
Obect-Oriented CollaborationAlena Holligan
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3Toni Kolev
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classesKumar
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodeSWIFTotter Solutions
 
Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designJean Michel
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibilitymachuga
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboardsDenis Ristic
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5Jason Austin
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeDhivyaa C.R
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Fabien Potencier
 

Similar to Demystifying Object-Oriented Programming - Midwest PHP (20)

Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
 
Take the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpTake the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphp
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017
 
Obect-Oriented Collaboration
Obect-Oriented CollaborationObect-Oriented Collaboration
Obect-Oriented Collaboration
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better Code
 
Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Only oop
Only oopOnly oop
Only oop
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
 

More from Alena Holligan

2023 Longhorn PHP - Learn to Succeed .pdf
2023 Longhorn PHP - Learn to Succeed .pdf2023 Longhorn PHP - Learn to Succeed .pdf
2023 Longhorn PHP - Learn to Succeed .pdfAlena Holligan
 
Environmental variables
Environmental variablesEnvironmental variables
Environmental variablesAlena Holligan
 
Experiential Project Design
Experiential Project DesignExperiential Project Design
Experiential Project DesignAlena Holligan
 
Organization Patterns: MVC
Organization Patterns: MVCOrganization Patterns: MVC
Organization Patterns: MVCAlena Holligan
 
When & Why: Interfaces, abstract classes, traits
When & Why: Interfaces, abstract classes, traitsWhen & Why: Interfaces, abstract classes, traits
When & Why: Interfaces, abstract classes, traitsAlena Holligan
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressAlena Holligan
 
Exploiting the Brain for Fun and Profit
Exploiting the Brain for Fun and ProfitExploiting the Brain for Fun and Profit
Exploiting the Brain for Fun and ProfitAlena Holligan
 
Environmental Variables
Environmental VariablesEnvironmental Variables
Environmental VariablesAlena Holligan
 
Exploiting the Brain for Fun & Profit #zendcon2016
Exploiting the Brain for Fun & Profit #zendcon2016Exploiting the Brain for Fun & Profit #zendcon2016
Exploiting the Brain for Fun & Profit #zendcon2016Alena Holligan
 
Presentation Bulgaria PHP
Presentation Bulgaria PHPPresentation Bulgaria PHP
Presentation Bulgaria PHPAlena Holligan
 
Exploiting the Brain for Fun and Profit - Lone Star 2016
Exploiting the Brain for Fun and Profit - Lone Star 2016Exploiting the Brain for Fun and Profit - Lone Star 2016
Exploiting the Brain for Fun and Profit - Lone Star 2016Alena Holligan
 
Exploiting the Brain for Fun & Profit #ssphp16
Exploiting the Brain for Fun & Profit #ssphp16Exploiting the Brain for Fun & Profit #ssphp16
Exploiting the Brain for Fun & Profit #ssphp16Alena Holligan
 

More from Alena Holligan (20)

2023 Longhorn PHP - Learn to Succeed .pdf
2023 Longhorn PHP - Learn to Succeed .pdf2023 Longhorn PHP - Learn to Succeed .pdf
2023 Longhorn PHP - Learn to Succeed .pdf
 
Environmental variables
Environmental variablesEnvironmental variables
Environmental variables
 
Dev parent
Dev parentDev parent
Dev parent
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Dependency Management
Dependency ManagementDependency Management
Dependency Management
 
Experiential Project Design
Experiential Project DesignExperiential Project Design
Experiential Project Design
 
Reduce Reuse Refactor
Reduce Reuse RefactorReduce Reuse Refactor
Reduce Reuse Refactor
 
Organization Patterns: MVC
Organization Patterns: MVCOrganization Patterns: MVC
Organization Patterns: MVC
 
When & Why: Interfaces, abstract classes, traits
When & Why: Interfaces, abstract classes, traitsWhen & Why: Interfaces, abstract classes, traits
When & Why: Interfaces, abstract classes, traits
 
Object Features
Object FeaturesObject Features
Object Features
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
 
Let's Talk Scope
Let's Talk ScopeLet's Talk Scope
Let's Talk Scope
 
Exploiting the Brain for Fun and Profit
Exploiting the Brain for Fun and ProfitExploiting the Brain for Fun and Profit
Exploiting the Brain for Fun and Profit
 
Environmental Variables
Environmental VariablesEnvironmental Variables
Environmental Variables
 
Learn to succeed
Learn to succeedLearn to succeed
Learn to succeed
 
Exploiting the Brain for Fun & Profit #zendcon2016
Exploiting the Brain for Fun & Profit #zendcon2016Exploiting the Brain for Fun & Profit #zendcon2016
Exploiting the Brain for Fun & Profit #zendcon2016
 
Presentation Bulgaria PHP
Presentation Bulgaria PHPPresentation Bulgaria PHP
Presentation Bulgaria PHP
 
Presentation pnwphp
Presentation pnwphpPresentation pnwphp
Presentation pnwphp
 
Exploiting the Brain for Fun and Profit - Lone Star 2016
Exploiting the Brain for Fun and Profit - Lone Star 2016Exploiting the Brain for Fun and Profit - Lone Star 2016
Exploiting the Brain for Fun and Profit - Lone Star 2016
 
Exploiting the Brain for Fun & Profit #ssphp16
Exploiting the Brain for Fun & Profit #ssphp16Exploiting the Brain for Fun & Profit #ssphp16
Exploiting the Brain for Fun & Profit #ssphp16
 

Recently uploaded

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Recently uploaded (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

Demystifying Object-Oriented Programming - Midwest PHP

  • 1. DEMYSTIFYING OBJECT-ORIENTED PROGRAMMING MAKING SENSE OF ALL THE MUMBO JUMBO HTTPS://GITHUB.COM/SKETCHINGS/OOP-BASICS
  • 3. TERMS • Class (properties, methods) • Object • Instance • Abstraction • Encapsulation
  • 4. TERMS CONTINUED • Polymorphism • Inheritance • Interface • Abstract Class • Traits • Namespaces
  • 5. CLASS • A template/blueprint that facilitates creation of objects. A set of program statements to do a certain task. Usually represents a noun, such as a person, place or thing. • Includes properties and methods — which are class functions
  • 6. OBJECT • Instance of a class. • In the real world object is a material thing that can be seen and touched. • In OOP, object is a self-contained entity that consists of both data and procedures.
  • 7. INSTANCE • Single occurrence/copy of an object • There might be one or several objects, but an instance is a specific copy, to which you can have a reference
  • 8. class User { //class
 private $name; //property
 public getName() { //method
 echo $this->name;
 }
 } $user1 = new User(); //first instance of object $user2 = new User(); //second instance of object
  • 9. ABSTRACTION • “An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.”
 — G. Booch • This is the class architecture itself. • Use something without knowing exactly how it works
  • 10. ENCAPSULATION • Scope. Controls who can access what. Restricting access to some of the object’s components (properties and methods), preventing unauthorized access. • Public - everyone • Protected - inherited classes • Private - class itself, not children
  • 11. class User {
 protected $name;
 protected $title;
 public function getFormattedSalutation() {
 return $this->getSalutation();
 }
 protected function getSalutation() {
 return $this->title . " " . $this->name;
 }
 public function getName() {
 return $this->name;
 }
 public function setName($name) {
 $this->name = $name;
 }
 public function getTitle() {
 return $this->title;
 }
 public function setTitle($title) {
 $this->title = $title;
 }
 }
  • 12. CREATING / USING THE OBJECT INSTANCE $user = new User();
 $user->setName("Jane Smith");
 $user->setTitle("Ms");
 echo $user->getFormattedSalutation(); When the script is run, it will return: Ms Jane Smith
  • 13. CONSTRUCTOR METHOD & MAGIC METHODS class User {
 protected $name;
 protected $title;
 
 public function __construct($name, $title) {
 $this->name = $name;
 $this->title = $title;
 }
 
 public function __toString() {
 return $this->getFormattedSalutation();
 }
 ...
 } For more see http://php.net/manual/en/language.oop5.magic.php
  • 14. CREATING / USING THE OBJECT INSTANCE $user = new User("Jane Smith","Ms");
 echo $user; When the script is run, it will return the same result as before: Ms Jane Smith
  • 16. INHERITANCE: PASSES KNOWLEDGE DOWN • Subclass, parent and a child relationship, allows for reusability, extensibility. • Additional code to an existing class without modifying it. Uses keyword “extends” • NUTSHELL: create a new class based on an existing class with more data, create new objects based on this class
  • 17. CREATING AND USING A CHILD CLASS class Developer extends User {
 public $skills = array();
 public function getSalutation() {
 return $this->title . " " . $this->name. ", Developer";
 }
 public function getSkillsString() {
 echo implode(", ",$this->skills);
 }
 } $developer = new Developer("Jane Smith", "Ms");
 echo $developer;
 echo "<br />";
 $developer->skills = array("JavasScript", "HTML", "CSS");
 $developer->skills[] = "PHP";
 $developer->getSkillsString();
  • 18. WHEN RUN, THE SCRIPT RETURNS: Ms Jane Smith, Developer JavasScript, HTML, CSS, PHP
  • 19. INTERFACE • Interface, specifies which methods a class must implement. • All methods in interface must be public. • Multiple interfaces can be implemented by using comma separation • Interface may contain a CONSTANT, but may not be overridden by implementing class
  • 20. interface UserInterface { public function getFormattedSalutation(); public function getName(); public function setName($name); public function getTitle(); public function setTitle($title); } class User implements UserInterface { … }
  • 21. ABSTRACT CLASS An abstract class is a mix between an interface and a class. It can define functionality as well as interface. Classes extending an abstract class must implement all of the abstract methods defined in the abstract class.
  • 22. abstract class User { //class public $name; //property public getName() { //method echo $this->name; } abstract public function setName($name); } class Developer extends User { … }
  • 24. TRAITS • Multiple traits can be implemented
  • 25. CREATING TRAITS trait Toolkit {
 public $tools = array();
 public function setTools($task) {
 switch {
 case “eat":
 $this->tools[] = ("Spoon", "Fork", "Knife");
 exit;
 ...
 }
 }
 public function showTools() {
 echo implode(", ",$this->skills);
 }
 }
  • 26. USING TRAITS class Developer extends User {
 use Toolkit;
 
 ...
 } $developer = new Developer("Jane Smith", "Ms");
 echo $developer;
 echo "<br />";
 $developer->setTools("Eat");
 echo $developer->showTools();
  • 27. WHEN RUN, THE SCRIPT RETURNS: Ms Jane Smith Spoon, Fork, Knife
  • 29. NAMESPACES • Help create a new layer of code encapsulation • Keep properties from colliding between areas of your code • Only classes, interfaces, functions and constants are affected • Anything that does not have a namespace is considered in the Global namespace (namespace = "")
  • 30. NAMESPACES • Must be declared first (except 'declare) • Can define multiple in the same file • You can define that something be used in the "Global" namespace by enclosing a non-labeled namespace in {} brackets. • Use namespaces from within other namespaces, along with aliasing
  • 31. • namespace myUser; • class User { //class • public $name; //property • public getName() { //method • echo $this->name; • } • public function setName($name); • } • class Developer extends myUserUser { … }
  • 33. ALENA HOLLIGAN @sketchings
 sketchings.com Wife and Mother of 3 young children 
 PHP Teacher at Treehouse
 PHP Users Group Leader
 Women Who Code Leader https://joind.in/talk/6b753
  • 34. CHALLENGES 1. Change to User class to an abstract class. 2. Throw an error because your access is too restricted. 3. Extend the User class for another type of user, such as our Developer example 4. Define 2 “User” classes in one file using namespacing https://github.com/sketchings/oop-basics
  • 35. RESOURCES • LeanPub: The Essentials of Object Oriented PHP • Head First Object-Oriented Analysis and Design