SlideShare una empresa de Scribd logo
1 de 49
Descargar para leer sin conexión
Demystifying

Object-Oriented Programming
Download Files:

https://github.com/sketchings/oop-basics
https://joind.in/talk/153b4
Presented by: Alena Holligan
• Wife and Mother of 3 young children
• PHP Teacher at Treehouse
• Group Leader (PHPDX, Women Who Code Portland)
www.sketchings.com

@sketchings

alena@holligan.us
Terminology
the single most important part
PART 1: Terms
Class (properties, methods)
Object
Instance
Abstraction
Encapsulation
PART 2: Polymorphism
Inheritance
Interface
Abstract Class
Traits
Part 3: ADDED FEATURES
Namespaces
Type Declarations
Static Methods
Magic Methods
Magic Constants
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 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
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
Team-up
oop is great for working in groups
Challenges
1. Create a new class with properties and methods
2. Instantiate a new user with a different name and title
3. Throw an error because your access is too
restricted.
https://github.com/sketchings/oop-basics
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 getSalutation() {//override method

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

}

public function getSkillsString(){ //additional method

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

}

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

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

$developer->setTitle(“Ms”);
echo $developer->getFormatedSalutation();

echo "<br />”;
$developer->skills = array("JavasScript", "HTML", "CSS");

$developer->skills[] = “PHP";
echo $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); //abstract method

}
class Developer extends User {

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

…
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");

exit;

...

}

}

public function showTools() {

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

}

}
Using Traits
class Developer extends User {

use Toolkit;

...

}
$developer = new Developer();

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

$developer->setTitle(”Ms”);

echo $developer;

echo "<br />";

$developer->setTools("Eat");

echo $developer->showTools();
When run, the script returns:
Ms Jane Smith
Spoon, Fork, Knife
Challenges
1. Change to User class to an abstract class.
2. Extend the User class for another type of user, such as
our Developer example
3. Add an Interface for the Developer Class

(or your own class)
4. Add a trait to the User
https://github.com/sketchings/oop-basics
Part 3: Added Features
Namespaces
Type Declarations
Magic Methods
Magic Constants
Static Methods
Namespaces
Prevent Code Collision
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 { … }
Available Type Declarations
PHP 5.4
Class/Interface,
self, array,
callable
PHP 7
bool
float
int
string
Type Declarations
class Conference {

public $title;

private $attendees = array();

public function addAttendee(User $person) {

$this->attendees[] = $person;

}

public function getAttendees(): array {

foreach($this->attendees as $person) {

$attendee_list[] = $person; 

}

return $attendee_list;

}

}
Using Type Declarations
$zendcon = new Conference();

$zendcon->title = ”ZendCon 2016”;

$zendcon->addAttendee($user);

echo implode(", “, $zendcon->getAttendees());
When the script is run, it will return the same result as before:
Ms Jane Smith
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
Magic Methods and Constants
class User {

protected $name;

protected $title;



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 the same result as
before:
User: Ms Jane Smith
Adding a Static Methods
class User {

public $encouragements = array(

“You are beautiful!”,

“You have this!”,



public static function encourage()

{

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

return $this->encouragements[$int];

}

...

}
Using the Static Method
echo User::encourage();
When the script is run, it will return the same result
as before:
You have this!
Challenges
1. Define 2 “User” classes. Use both classes in one file using
namespacing
2. Try defining types AND try accepting/returning the wrong types
3. Try another Magic Method http://php.net/manual/en/language.oop5.magic.php
4. Add Magic Constants http://php.net/manual/en/
language.constants.predefined.php
5. Add and use a Static Method
https://github.com/sketchings/oop-basics
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
• Group Leader (PHPDX, Women Who Code Portland)
www.sketchings.com

@sketchings

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

Más contenido relacionado

La actualidad más candente

The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015Fernando Hamasaki de Amorim
 
Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)James Titcumb
 
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...King Foo
 
Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...Puppet
 
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)James Titcumb
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principlesPerl Careers
 
PECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterZendCon
 
Writing and using php streams and sockets tek11
Writing and using php streams and sockets   tek11Writing and using php streams and sockets   tek11
Writing and using php streams and sockets tek11Elizabeth Smith
 
Php on the Web and Desktop
Php on the Web and DesktopPhp on the Web and Desktop
Php on the Web and DesktopElizabeth Smith
 
Symfony2 Service Container: Inject me, my friend
Symfony2 Service Container: Inject me, my friendSymfony2 Service Container: Inject me, my friend
Symfony2 Service Container: Inject me, my friendKirill Chebunin
 
Php on the desktop and php gtk2
Php on the desktop and php gtk2Php on the desktop and php gtk2
Php on the desktop and php gtk2Elizabeth Smith
 
mod_rewrite bootcamp, Ohio LInux 2011
mod_rewrite bootcamp, Ohio LInux 2011mod_rewrite bootcamp, Ohio LInux 2011
mod_rewrite bootcamp, Ohio LInux 2011Rich Bowen
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objectsjulien pauli
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)James Titcumb
 

La actualidad más candente (20)

TDD with PhpSpec
TDD with PhpSpecTDD with PhpSpec
TDD with PhpSpec
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
 
Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)
 
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
 
Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...Replacing "exec" with a type and provider: Return manifests to a declarative ...
Replacing "exec" with a type and provider: Return manifests to a declarative ...
 
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
 
PECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life better
 
Writing and using php streams and sockets tek11
Writing and using php streams and sockets   tek11Writing and using php streams and sockets   tek11
Writing and using php streams and sockets tek11
 
Ant
Ant Ant
Ant
 
Php on the Web and Desktop
Php on the Web and DesktopPhp on the Web and Desktop
Php on the Web and Desktop
 
Symfony2 Service Container: Inject me, my friend
Symfony2 Service Container: Inject me, my friendSymfony2 Service Container: Inject me, my friend
Symfony2 Service Container: Inject me, my friend
 
Php on the desktop and php gtk2
Php on the desktop and php gtk2Php on the desktop and php gtk2
Php on the desktop and php gtk2
 
mod_rewrite bootcamp, Ohio LInux 2011
mod_rewrite bootcamp, Ohio LInux 2011mod_rewrite bootcamp, Ohio LInux 2011
mod_rewrite bootcamp, Ohio LInux 2011
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 
Spl in the wild
Spl in the wildSpl in the wild
Spl in the wild
 
Php go vrooom!
Php go vrooom!Php go vrooom!
Php go vrooom!
 
TDD with phpspec2
TDD with phpspec2TDD with phpspec2
TDD with phpspec2
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 

Destacado

Demystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHPDemystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHPAlena Holligan
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Clark Everetts
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaGlenn Guden
 
Your First Magento 2 Module
Your First Magento 2 ModuleYour First Magento 2 Module
Your First Magento 2 ModuleBen Marks
 
DB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM iDB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM iAlan Seiden
 
All the Laravel things: up and running to making $$
All the Laravel things: up and running to making $$All the Laravel things: up and running to making $$
All the Laravel things: up and running to making $$Joe Ferguson
 
WordPress for the modern PHP developer
WordPress for the modern PHP developerWordPress for the modern PHP developer
WordPress for the modern PHP developerChris Sherry
 

Destacado (7)

Demystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHPDemystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHP
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using Java
 
Your First Magento 2 Module
Your First Magento 2 ModuleYour First Magento 2 Module
Your First Magento 2 Module
 
DB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM iDB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM i
 
All the Laravel things: up and running to making $$
All the Laravel things: up and running to making $$All the Laravel things: up and running to making $$
All the Laravel things: up and running to making $$
 
WordPress for the modern PHP developer
WordPress for the modern PHP developerWordPress for the modern PHP developer
WordPress for the modern PHP developer
 

Similar a Demystifying Object-Oriented Programming - ZendCon 2016

Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPDemystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPAlena 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
 
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
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Alena Holligan
 
Obect-Oriented Collaboration
Obect-Oriented CollaborationObect-Oriented Collaboration
Obect-Oriented CollaborationAlena 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
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Alena Holligan
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5Jason Austin
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classesKumar
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibilitymachuga
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHPwahidullah mudaser
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxAtikur Rahman
 
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
 

Similar a Demystifying Object-Oriented Programming - ZendCon 2016 (20)

Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPDemystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHP
 
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
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
 
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
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Obect-Oriented Collaboration
Obect-Oriented CollaborationObect-Oriented Collaboration
Obect-Oriented Collaboration
 
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
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
 
Only oop
Only oopOnly oop
Only oop
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptx
 
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
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 

Más de 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
 

Más de 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
 

Último

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 

Último (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

Demystifying Object-Oriented Programming - ZendCon 2016

  • 2. Presented by: Alena Holligan • Wife and Mother of 3 young children • PHP Teacher at Treehouse • Group Leader (PHPDX, Women Who Code Portland) www.sketchings.com
 @sketchings
 alena@holligan.us
  • 4. PART 1: Terms Class (properties, methods) Object Instance Abstraction Encapsulation
  • 6. Part 3: ADDED FEATURES Namespaces Type Declarations Static Methods Magic Methods Magic Constants
  • 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
 private $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. 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
  • 14. 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;
 }
 }
  • 15. 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
  • 16. Team-up oop is great for working in groups
  • 17. Challenges 1. Create a new class with properties and methods 2. Instantiate a new user with a different name and title 3. Throw an error because your access is too restricted. https://github.com/sketchings/oop-basics
  • 19. pol·y·mor·phism /ˌpälēˈmôrfizəm/ The condition of occurring in several different forms BIOLOGY GENETICS BIOCHEMISTRY COMPUTING
  • 21. 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
  • 22. Creating a child class class Developer extends User {
 public $skills = array(); //additional property public function getSalutation() {//override method
 return $this->title . " " . $this->name. ", Developer";
 }
 public function getSkillsString(){ //additional method
 return implode(", ",$this->skills);
 }
 }
  • 23. Using a child class $developer = new Developer();
 $developer->setName(”Jane Smith”);
 $developer->setTitle(“Ms”); echo $developer->getFormatedSalutation();
 echo "<br />”; $developer->skills = array("JavasScript", "HTML", "CSS");
 $developer->skills[] = “PHP"; echo $developer->getSkillsString();
  • 24. When run, the script returns: Ms Jane Smith, Developer JavasScript, HTML, CSS, PHP
  • 25. 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
  • 26. interface UserInterface { public function getFormattedSalutation(); public function getName(); public function setName($name); public function getTitle(); public function setTitle($title); } class User implements UserInterface { … }
  • 27. 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.
  • 28. abstract class User { //class public $name; //property public getName() { //method
 echo $this->name;
 } abstract public function setName($name); //abstract method
 } class Developer extends User {
 public setName($name) { //implementing the method
 …
  • 30. Creating Traits trait Toolkit {
 public $tools = array();
 public function setTools($task) {
 switch ($task) {
 case “eat":
 $this->tools[] = 
 array("Spoon", "Fork", "Knife");
 exit;
 ...
 }
 }
 public function showTools() {
 return implode(", ",$this->skills);
 }
 }
  • 31. Using Traits class Developer extends User {
 use Toolkit;
 ...
 } $developer = new Developer();
 $developer->setName(”Jane Smith”);
 $developer->setTitle(”Ms”);
 echo $developer;
 echo "<br />";
 $developer->setTools("Eat");
 echo $developer->showTools();
  • 32. When run, the script returns: Ms Jane Smith Spoon, Fork, Knife
  • 33. Challenges 1. Change to User class to an abstract class. 2. Extend the User class for another type of user, such as our Developer example 3. Add an Interface for the Developer Class
 (or your own class) 4. Add a trait to the User https://github.com/sketchings/oop-basics
  • 34. Part 3: Added Features Namespaces Type Declarations Magic Methods Magic Constants Static Methods
  • 35. Namespaces Prevent Code Collision 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 = "")
  • 36. 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
  • 37. namespace myUser; class User { //class public $name; //property public getName() { //method echo $this->name; } public function setName($name); } class Developer extends myUserUser { … }
  • 38. Available Type Declarations PHP 5.4 Class/Interface, self, array, callable PHP 7 bool float int string
  • 39. Type Declarations class Conference {
 public $title;
 private $attendees = array();
 public function addAttendee(User $person) {
 $this->attendees[] = $person;
 }
 public function getAttendees(): array {
 foreach($this->attendees as $person) {
 $attendee_list[] = $person; 
 }
 return $attendee_list;
 }
 }
  • 40. Using Type Declarations $zendcon = new Conference();
 $zendcon->title = ”ZendCon 2016”;
 $zendcon->addAttendee($user);
 echo implode(", “, $zendcon->getAttendees()); When the script is run, it will return the same result as before: Ms Jane Smith
  • 41. 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
  • 42. Magic Constants Predefined functions in PHP For more see http://php.net/manual/en/ language.constants.predefined.php
  • 43. Magic Methods and Constants class User {
 protected $name;
 protected $title;
 
 public function __construct($name, $title) {
 $this->name = $name;
 $this->title = $title;
 }
 
 public function __toString() {
 return __CLASS__. “: “
 . $this->getFormattedSalutation();
 }
 ...
 }
  • 44. Creating / Using the Magic Method $user = new User("Jane Smith","Ms");
 echo $user; When the script is run, it will return the same result as before: User: Ms Jane Smith
  • 45. Adding a Static Methods class User {
 public $encouragements = array(
 “You are beautiful!”,
 “You have this!”,
 
 public static function encourage()
 {
 $int = rand(count($this->encouragements));
 return $this->encouragements[$int];
 }
 ...
 }
  • 46. Using the Static Method echo User::encourage(); When the script is run, it will return the same result as before: You have this!
  • 47. Challenges 1. Define 2 “User” classes. Use both classes in one file using namespacing 2. Try defining types AND try accepting/returning the wrong types 3. Try another Magic Method http://php.net/manual/en/language.oop5.magic.php 4. Add Magic Constants http://php.net/manual/en/ language.constants.predefined.php 5. Add and use a Static Method https://github.com/sketchings/oop-basics
  • 48. Resources LeanPub: The Essentials of Object Oriented PHP Head First Object-Oriented Analysis and Design
  • 49. Presented by: Alena Holligan • Wife and Mother of 3 young children • PHP Teacher at Treehouse • Group Leader (PHPDX, Women Who Code Portland) www.sketchings.com
 @sketchings
 alena@holligan.us Download Files: https://github.com/sketchings/oop-basics https://joind.in/talk/153b4