SlideShare una empresa de Scribd logo
1 de 46
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Object oriented programming in php
Object oriented programming in phpObject oriented programming in php
Object oriented programming in php
Aashiq Kuchey
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
Rajesh S
 

La actualidad más candente (20)

Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
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
 
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 - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHPDemystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHP
 
Object oriented programming in php
Object oriented programming in phpObject oriented programming in php
Object oriented programming in php
 
PHP- Introduction to Object Oriented PHP
PHP-  Introduction to Object Oriented PHPPHP-  Introduction to Object Oriented PHP
PHP- Introduction to Object Oriented PHP
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards
 
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
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
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
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 
Oops concept in php
Oops concept in phpOops concept in php
Oops concept in php
 

Similar a Demystifying oop

Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
Kumar
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
Michael Peacock
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
rani marri
 
Advanced php
Advanced phpAdvanced php
Advanced php
hamfu
 

Similar a Demystifying oop (20)

Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
 
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
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16
 
Obect-Oriented Collaboration
Obect-Oriented CollaborationObect-Oriented Collaboration
Obect-Oriented Collaboration
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfObject_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdf
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptx
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
Let's Talk Scope
Let's Talk ScopeLet's Talk Scope
Let's Talk Scope
 
Web 9 | OOP in PHP
Web 9 | OOP in PHPWeb 9 | OOP in PHP
Web 9 | OOP in PHP
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 

Más de Alena 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
 
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
 
Exploiting the Brain for Fun & Profit #dc4d 2016
Exploiting the Brain for Fun & Profit #dc4d 2016Exploiting the Brain for Fun & Profit #dc4d 2016
Exploiting the Brain for Fun & Profit #dc4d 2016
 

Último

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
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
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
Safe Software
 

Último (20)

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...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
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
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
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
 

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