Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Demystifying oop

108 views

Published on

Abstraction, Encapsulation, Polymorphism, and Interfaces: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole Object-Oriented thing is WAY more complicated. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session we’ll take you through real life examples of Object-Oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilizing OOP immediately.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Demystifying oop

  1. 1. Demystifying
 Object-Oriented Programming .
  2. 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. 3. PART 1: Terminology Class (properties, methods, this) Object Instance Abstraction Encapsulation
  4. 4. PART 2: Polymorphism Inheritance Interface Abstract Class Traits
  5. 5. PART 3: Magic Magic Methods Magic Constants Static Properties and Methods
  6. 6. PART 1: Terminology
  7. 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. 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. 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. 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. 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. 12. Encapsulation Binds together the data and functions that manipulate the data, and keeps both safe from outside interference and misuse. Properties Methods
  13. 13. Team-up OOP is great for working in groups
  14. 14. Team Challenges Write a class with properties and methods Example: User class with name, title, and salutation
  15. 15. PART 2: Polymorphism D-R-Y
 Sharing Code
  16. 16. pol·y·mor·phism /ˌpälēˈmôrfizəm/ The condition of occurring in several different forms BIOLOGY GENETICS BIOCHEMISTRY COMPUTING
  17. 17. Terms Polymorphism Inheritance Interface Abstract Class Traits
  18. 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. 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. 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. 21. Team Challenges Extend the User class for another type of user, such as our Developer example
  22. 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. 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. 24. Team Challenges Add an Interface for your Class
  25. 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. 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. 27. Team Challenges Change to User class to an abstract class. Try instantiating the User class
  28. 28. Traits Composition Horizontal Code Reuse Multiple traits can be implemented
  29. 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. 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. 31. When run, the script returns: Ms Jane Smith Spoon, Fork, Knife
  32. 32. Team Challenges Add a trait to the User
  33. 33. PART 3: Magic Magic Methods Magic Constants Static Properties and Methods
  34. 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. 35. Magic Constants Predefined functions in PHP For more see http://php.net/manual/en/ language.constants.predefined.php
  36. 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. 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. 38. Challenges Add a Magic Method Add a Magic Constants
  39. 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. 40. Using the Static Method echo User::encourage(); When the script is run, it will return: You have this!
  41. 41. Team Challenge Add and use a Static Method
  42. 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. 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. 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. 45. Resources LeanPub: The Essentials of Object Oriented PHP Head First Object-Oriented Analysis and Design
  46. 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

×