SlideShare a Scribd company logo
Demystifying

Object-Oriented Programming
.
Presented by: Alena Holligan
• Wife, and Mother of 3 young children
• PHP Teacher at Treehouse
• Portland PHP User Group Leader
www.sketchings.com

@sketchings

alena@holligan.us
PART 1: Terminology
Class (properties, methods, this)
Object
Instance
Abstraction
Encapsulation
PART 2: Polymorphism
Inheritance
Interface
Abstract Class
Traits
PART 3: Magic
Magic Methods
Magic Constants
Static Properties and Methods
PART 1: Terminology
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

public $name; //property

public function getName() { //method

echo $this->name; //current object property

}

}
$user1 = new User(); //first instance of object
$user2 = new User(); //second instance of object
Abstraction
Managing the complexity of the system
Dealing with ideas rather than events
This is the class architecture itself.
Use something without knowing inner workings
Encapsulation
Binds together the data
and functions that
manipulate the data, and
keeps both safe from
outside interference and
misuse.
Properties
Methods
Team-up
OOP is great for working in groups
Team Challenges
Write a class with properties and methods
Example: User class with name, title, and salutation
PART 2:
Polymorphism
D-R-Y

Sharing Code
pol·y·mor·phism
/ˌpälēˈmôrfizəm/
The condition of occurring in several different forms
BIOLOGY
GENETICS
BIOCHEMISTRY
COMPUTING
Terms
Polymorphism
Inheritance
Interface
Abstract Class
Traits
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 a child class
class Developer extends User {

public $skills = array(); //additional property
public function getSkillsString(){ //additional method

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

}
public function getSalutation() {//override method

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

}

}
Using a child class
$developer = new Developer();

$developer->setName(”Jane Smith”);

$developer->setTitle(“Ms”);

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

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

echo $developer->getSkillsString();
When the script is run, it will return:
Ms Jane Smith, Developer
JavasScript, HTML, CSS, PHP
Team Challenges
Extend the User class for another type of user, such as our
Developer example
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 { … }
Team Challenges
Add an Interface for your Class
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 { //abstract class
public $name; //class property
public getName() { //class method

echo $this->name;

}
abstract public function setName($name); //abstract method

}
class Developer extends User {

public setName($name) { //implementing the method

…
Team Challenges
Change to User class to an abstract class.
Try instantiating the User class
Traits
Composition
Horizontal Code Reuse
Multiple traits can be implemented
Creating Traits
trait Toolkit {

public $tools = array();

public function setTools($task) {

switch ($task) {

case “eat":

$this->tools = 

array("Spoon", "Fork", "Knife");

break;

...

}

}

public function showTools() {

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

}

}
Using Traits
class Developer extends User {

use Toolkit;

...

}
$developer = new Developer();

$developer->setTools("Eat");

echo $developer->showTools();
When run the script returns:
Spoon, Fork, Knife
When run, the script returns:
Ms Jane Smith
Spoon, Fork, Knife
Team Challenges
Add a trait to the User
PART 3: Magic
Magic Methods
Magic Constants
Static Properties and Methods
Magic Methods
Setup just like any other method
The Magic comes from the fact that they are
triggered and not called
For more see http://php.net/manual/en/
language.oop5.magic.php
Magic Constants
Predefined functions in PHP
For more see http://php.net/manual/en/
language.constants.predefined.php
Using Magic Methods and Constants
class User {
...



public function __construct($name, $title) {

$this->name = $name;

$this->title = $title;

}



public function __toString() {

return __CLASS__. “: “

. $this->getFormattedSalutation();

}

...

}
Creating / Using the Magic Method
$user = new User("Jane Smith","Ms");

echo $user;
When the script is run, it will return:
User: Ms Jane Smith
Challenges
Add a Magic Method
Add a Magic Constants
Static Properties and Methods
class User {

public static $encouragements = array(

“You are beautiful!”,

“You have this!”,



public static function encourage()

{

$int = rand(count($this->encouragements));

return self::encouragements[$int];

}

...

}
Using the Static Method
echo User::encourage();
When the script is run, it will return:
You have this!
Team Challenge
Add and use a Static Method
The FINAL Keyword
Prevents child classes from overriding a method by
prefixing the definition with final.
If the class itself is being defined final then it cannot
be extended.
class User { //can extend

final public getName() { //cannot extend

echo $this->name;

}
final class Developer extends User { //cannot extend

public getName() { //error

echo “My Name is: ” .$this->name;

}
class Manager extends Developer {…} //error
Team Challenges
Define a parent method as FINAL and try to override in the child
Define a class as FINAL and try to create a child
Resources
LeanPub: The Essentials of Object Oriented PHP
Head First Object-Oriented Analysis and Design
Presented by: Alena Holligan
• Wife and Mother of 3 young children
• PHP Teacher at Treehouse
• Portland PHP User Group Leader
www.sketchings.com

@sketchings

alena@holligan.us
Download Files: https://github.com/sketchings/oop-basics
https://joind.in/talk/131c4

More Related Content

PPTX
Only oop
PDF
Demystifying Object-Oriented Programming - PHP[tek] 2017
PDF
Demystifying Object-Oriented Programming #phpbnl18
PPTX
Object oreinted php | OOPs
PPT
Php Oop
PPTX
Php oop presentation
PPT
Oops concepts in php
Only oop
Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming #phpbnl18
Object oreinted php | OOPs
Php Oop
Php oop presentation
Oops concepts in php

What's hot (20)

PPT
Oops in PHP
PDF
OOP in PHP
PDF
Object Oriented Programming in PHP
PDF
Demystifying Object-Oriented Programming - Lone Star PHP
PPT
Class 7 - PHP Object Oriented Programming
PDF
Demystifying Object-Oriented Programming - Midwest PHP
PPTX
Object oriented programming in php
PPT
PHP- Introduction to Object Oriented PHP
PPTX
Object oriented programming in php 5
PDF
Object Oriented Programming with PHP 5 - More OOP
PDF
09 Object Oriented Programming in PHP #burningkeyboards
PDF
Demystifying Object-Oriented Programming - PHP UK Conference 2017
ZIP
Object Oriented PHP5
PPT
Class and Objects in PHP
PDF
Take the Plunge with OOP from #pnwphp
PPTX
Oop in-php
PPTX
Inheritance in java
PPT
Oops in PHP By Nyros Developer
DOCX
Oops concept in php
Oops in PHP
OOP in PHP
Object Oriented Programming in PHP
Demystifying Object-Oriented Programming - Lone Star PHP
Class 7 - PHP Object Oriented Programming
Demystifying Object-Oriented Programming - Midwest PHP
Object oriented programming in php
PHP- Introduction to Object Oriented PHP
Object oriented programming in php 5
Object Oriented Programming with PHP 5 - More OOP
09 Object Oriented Programming in PHP #burningkeyboards
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Object Oriented PHP5
Class and Objects in PHP
Take the Plunge with OOP from #pnwphp
Oop in-php
Inheritance in java
Oops in PHP By Nyros Developer
Oops concept in php
Ad

Similar to Demystifying oop (20)

PDF
Demystifying Object-Oriented Programming - ZendCon 2016
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
PPTX
UNIT III (8).pptx
PPTX
UNIT III (8).pptx
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
PPT
Php object orientation and classes
PPT
Introduction to OOP with PHP
PDF
Demystifying Object-Oriented Programming #ssphp16
PDF
Obect-Oriented Collaboration
PDF
OOP in PHP
PPTX
Ch8(oop)
PDF
Object_oriented_programming_OOP_with_PHP.pdf
PDF
Object Oriented Programming in PHP
PPTX
OOPS IN PHP.pptx
PPT
Advanced php
PPTX
Lecture 17 - PHP-Object-Orientation.pptx
PPTX
PHP OOP Lecture - 04.pptx
PPT
Synapseindia object oriented programming in php
PDF
Let's Talk Scope
PDF
Web 9 | OOP in PHP
Demystifying Object-Oriented Programming - ZendCon 2016
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
UNIT III (8).pptx
UNIT III (8).pptx
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Php object orientation and classes
Introduction to OOP with PHP
Demystifying Object-Oriented Programming #ssphp16
Obect-Oriented Collaboration
OOP in PHP
Ch8(oop)
Object_oriented_programming_OOP_with_PHP.pdf
Object Oriented Programming in PHP
OOPS IN PHP.pptx
Advanced php
Lecture 17 - PHP-Object-Orientation.pptx
PHP OOP Lecture - 04.pptx
Synapseindia object oriented programming in php
Let's Talk Scope
Web 9 | OOP in PHP
Ad

More from Alena Holligan (20)

PDF
2023 Longhorn PHP - Learn to Succeed .pdf
PDF
Environmental variables
PDF
Dev parent
PDF
Dependency Injection
PDF
Dependency Management
PDF
Experiential Project Design
PDF
Reduce Reuse Refactor
PDF
Organization Patterns: MVC
PDF
When & Why: Interfaces, abstract classes, traits
PDF
Object Features
PDF
WordCamp Portland 2018: PHP for WordPress
PDF
Exploiting the Brain for Fun and Profit
PDF
Environmental Variables
PDF
Learn to succeed
PDF
Exploiting the Brain for Fun & Profit #zendcon2016
PDF
Presentation Bulgaria PHP
PDF
Presentation pnwphp
PDF
Exploiting the Brain for Fun and Profit - Lone Star 2016
PDF
Exploiting the Brain for Fun & Profit #ssphp16
PDF
Exploiting the Brain for Fun & Profit #dc4d 2016
2023 Longhorn PHP - Learn to Succeed .pdf
Environmental variables
Dev parent
Dependency Injection
Dependency Management
Experiential Project Design
Reduce Reuse Refactor
Organization Patterns: MVC
When & Why: Interfaces, abstract classes, traits
Object Features
WordCamp Portland 2018: PHP for WordPress
Exploiting the Brain for Fun and Profit
Environmental Variables
Learn to succeed
Exploiting the Brain for Fun & Profit #zendcon2016
Presentation Bulgaria PHP
Presentation pnwphp
Exploiting the Brain for Fun and Profit - Lone Star 2016
Exploiting the Brain for Fun & Profit #ssphp16
Exploiting the Brain for Fun & Profit #dc4d 2016

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
Teaching material agriculture food technology
PDF
Encapsulation theory and applications.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Cloud computing and distributed systems.
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Teaching material agriculture food technology
Encapsulation theory and applications.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Machine learning based COVID-19 study performance prediction
NewMind AI Weekly Chronicles - August'25 Week I
20250228 LYD VKU AI Blended-Learning.pptx
Cloud computing and distributed systems.
MYSQL Presentation for SQL database connectivity
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Network Security Unit 5.pdf for BCA BBA.
Encapsulation_ Review paper, used for researhc scholars
Mobile App Security Testing_ A Comprehensive Guide.pdf
Modernizing your data center with Dell and AMD
The Rise and Fall of 3GPP – Time for a Sabbatical?

Demystifying oop

  • 2. Presented by: Alena Holligan • Wife, and Mother of 3 young children • PHP Teacher at Treehouse • Portland PHP User Group Leader www.sketchings.com
 @sketchings
 alena@holligan.us
  • 3. PART 1: Terminology Class (properties, methods, this) Object Instance Abstraction Encapsulation
  • 5. PART 3: Magic Magic Methods Magic Constants Static Properties and Methods
  • 7. 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
  • 8. 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.
  • 9. 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
  • 10. class User { //class
 public $name; //property
 public function getName() { //method
 echo $this->name; //current object property
 }
 } $user1 = new User(); //first instance of object $user2 = new User(); //second instance of object
  • 11. Abstraction Managing the complexity of the system Dealing with ideas rather than events This is the class architecture itself. Use something without knowing inner workings
  • 12. Encapsulation Binds together the data and functions that manipulate the data, and keeps both safe from outside interference and misuse. Properties Methods
  • 13. Team-up OOP is great for working in groups
  • 14. Team Challenges Write a class with properties and methods Example: User class with name, title, and salutation
  • 16. pol·y·mor·phism /ˌpälēˈmôrfizəm/ The condition of occurring in several different forms BIOLOGY GENETICS BIOCHEMISTRY COMPUTING
  • 18. 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
  • 19. Creating a child class class Developer extends User {
 public $skills = array(); //additional property public function getSkillsString(){ //additional method
 return implode(", ",$this->skills);
 } public function getSalutation() {//override method
 return $this->title . " " . $this->name. ", Developer";
 }
 }
  • 20. Using a child class $developer = new Developer();
 $developer->setName(”Jane Smith”);
 $developer->setTitle(“Ms”);
 echo $developer->getFormatedSalutation(); $developer->skills = array("JavasScript", "HTML", "CSS");
 $developer->skills[] = “PHP";
 echo $developer->getSkillsString(); When the script is run, it will return: Ms Jane Smith, Developer JavasScript, HTML, CSS, PHP
  • 21. Team Challenges Extend the User class for another type of user, such as our Developer example
  • 22. 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
  • 23. interface UserInterface { public function getFormattedSalutation(); public function getName(); public function setName($name); public function getTitle(); public function setTitle($title); } class User implements UserInterface { … }
  • 24. Team Challenges Add an Interface for your Class
  • 25. 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.
  • 26. abstract class User { //abstract class public $name; //class property public getName() { //class method
 echo $this->name;
 } abstract public function setName($name); //abstract method
 } class Developer extends User {
 public setName($name) { //implementing the method
 …
  • 27. Team Challenges Change to User class to an abstract class. Try instantiating the User class
  • 29. Creating Traits trait Toolkit {
 public $tools = array();
 public function setTools($task) {
 switch ($task) {
 case “eat":
 $this->tools = 
 array("Spoon", "Fork", "Knife");
 break;
 ...
 }
 }
 public function showTools() {
 return implode(", ",$this->skills);
 }
 }
  • 30. Using Traits class Developer extends User {
 use Toolkit;
 ...
 } $developer = new Developer();
 $developer->setTools("Eat");
 echo $developer->showTools(); When run the script returns: Spoon, Fork, Knife
  • 31. When run, the script returns: Ms Jane Smith Spoon, Fork, Knife
  • 32. Team Challenges Add a trait to the User
  • 33. PART 3: Magic Magic Methods Magic Constants Static Properties and Methods
  • 34. Magic Methods Setup just like any other method The Magic comes from the fact that they are triggered and not called For more see http://php.net/manual/en/ language.oop5.magic.php
  • 35. Magic Constants Predefined functions in PHP For more see http://php.net/manual/en/ language.constants.predefined.php
  • 36. Using Magic Methods and Constants class User { ...
 
 public function __construct($name, $title) {
 $this->name = $name;
 $this->title = $title;
 }
 
 public function __toString() {
 return __CLASS__. “: “
 . $this->getFormattedSalutation();
 }
 ...
 }
  • 37. Creating / Using the Magic Method $user = new User("Jane Smith","Ms");
 echo $user; When the script is run, it will return: User: Ms Jane Smith
  • 38. Challenges Add a Magic Method Add a Magic Constants
  • 39. Static Properties and Methods class User {
 public static $encouragements = array(
 “You are beautiful!”,
 “You have this!”,
 
 public static function encourage()
 {
 $int = rand(count($this->encouragements));
 return self::encouragements[$int];
 }
 ...
 }
  • 40. Using the Static Method echo User::encourage(); When the script is run, it will return: You have this!
  • 41. Team Challenge Add and use a Static Method
  • 42. The FINAL Keyword Prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
  • 43. class User { //can extend
 final public getName() { //cannot extend
 echo $this->name;
 } final class Developer extends User { //cannot extend
 public getName() { //error
 echo “My Name is: ” .$this->name;
 } class Manager extends Developer {…} //error
  • 44. Team Challenges Define a parent method as FINAL and try to override in the child Define a class as FINAL and try to create a child
  • 45. Resources LeanPub: The Essentials of Object Oriented PHP Head First Object-Oriented Analysis and Design
  • 46. Presented by: Alena Holligan • Wife and Mother of 3 young children • PHP Teacher at Treehouse • Portland PHP User Group Leader www.sketchings.com
 @sketchings
 alena@holligan.us Download Files: https://github.com/sketchings/oop-basics https://joind.in/talk/131c4