![Page 1: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/1.jpg)
WELCOME TO ‘INTRO TO OOP WITH PHP’
Thank you for your interest. Files can be found at https://github.com/sketchings/oop-basics
Contact Info:
www.sketchings.com
@sketchings
![Page 2: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/2.jpg)
INTRO TO OOP WITH PHP (INTRODUCTION)A BASIC LOOK INTO OBJECT-ORIENTED PROGRAMMING
![Page 3: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/3.jpg)
WHAT IS OOP?
➔Object-Oriented Programing
➔A programming concept that treats functions and data as objects.
➔A programming methodology based on objects, instead of functions and procedures
![Page 4: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/4.jpg)
OOP VS PROCEDURAL OR FUNCTIONAL
• OOP is built around the "nouns", the things in the system, and what they are capable of
• Whereas procedural or functional programming is built around the "verbs" of the system, the things you want the system to do
![Page 5: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/5.jpg)
LET’S GET INTO SOME EXAMPLES
Files can be found at https://github.com/sketchings/oop-basics
Contact Info:
www.sketchings.com
@sketchings
![Page 6: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/6.jpg)
WELCOME TO ‘INTRO TO OOP WITH PHP’ PART 1
Thank you for your interest. Files can be found at https://github.com/sketchings/oop-basics
Contact Info:
www.sketchings.com
@sketchings
![Page 7: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/7.jpg)
INTRO TO OOP WITH PHP (PART 1)A BASIC LOOK INTO OBJECT-ORIENTED PROGRAMMING
![Page 8: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/8.jpg)
TERMINOLOGYTHE SINGLE MOST IMPORTANT PART
![Page 9: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/9.jpg)
TERMS
• Class (properties, methods)
• Object
• Instance
• Abstraction
• Encapsulation
• Inheritance
![Page 10: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/10.jpg)
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
![Page 11: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/11.jpg)
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.
![Page 12: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/12.jpg)
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
![Page 13: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/13.jpg)
class User { //class
private $name; //property
public getName() { //method
echo $this->name;
}
}
$user1 = new User(); //first instance of object
$user2 = new User(); //second instance of object
![Page 14: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/14.jpg)
ABSTRACTION
• “An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.”— G. Booch
• This is the class architecture itself.
![Page 15: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/15.jpg)
ENCAPSULATION
• Scope. Controls who can access what. Restricting access to some of the object’s components (properties and methods), preventing unauthorized access.
• Public - everyone
• Protected - inherited classes
• Private - class itself, not children
![Page 16: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/16.jpg)
• 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; } }
![Page 17: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/17.jpg)
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
![Page 18: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/18.jpg)
CONSTRUCTOR METHOD & MAGIC METHODSclass User { protected $name; protected $title; public function __construct($name, $title) { $this->name = $name; $this->title = $title; } public function __toString() { return $this->getFormattedSalutation(); } ... }
For more see http://php.net/manual/en/language.oop5.magic.php
![Page 19: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/19.jpg)
CREATING / USING THE OBJECT INSTANCE
$user = new User("Jane Smith","Ms"); echo $user;
When the script is run, it will return the same result as before:
Ms Jane Smith
![Page 20: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/20.jpg)
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
![Page 21: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/21.jpg)
CREATING AND USING A CHILD CLASSclass Developer extends User { public $skills = array(); public function getSalutation() { return $this->title . " " . $this->name. ", Developer"; } public function getSkillsString() { echo implode(", ",$this->skills); } }
$developer = new Developer("Jane Smith", "Ms"); echo $developer; echo "<br />"; $developer->skills = array("JavasScript", "HTML", "CSS"); $developer->skills[] = "PHP"; $developer->getSkillsString();
![Page 22: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/22.jpg)
WHEN RUN, THE SCRIPT RETURNS:
Ms Jane Smith
JavasScript, HTML, CSS, PHP
![Page 23: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/23.jpg)
FINISHEDQUESTION AND ANSWER TIME
![Page 24: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/24.jpg)
THANK YOU FROM ALENA HOLLIGAN
Files at https://github.com/sketchings/oop-basics
Contact Info:
www.sketchings.com
@sketchings
![Page 25: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/25.jpg)
WELCOME TO ‘INTRO TO OOP WITH PHP’ PART 2Thank you for your interest. Files can be found at https://github.com/sketchings/oop-basics
Contact Info:
www.sketchings.com
@sketchings
![Page 26: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/26.jpg)
INTRO TO OOP WITH PHP (PART 2)A BASIC LOOK INTO OBJECT-ORIENTED PROGRAMMING
![Page 27: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/27.jpg)
TERMINOLOGYTHE SINGLE MOST IMPORTANT PART
![Page 28: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/28.jpg)
TERMS
• Class (properties, methods)
• Object
• Instance
• Abstraction
• Encapsulation
• Inheritance
![Page 29: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/29.jpg)
TERMS CONTINUED
• Polymorphism
• Interface
• Abstract
• Type Hinting
• Namespaces
![Page 30: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/30.jpg)
POLYMORPHISM
Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface
![Page 31: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/31.jpg)
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
![Page 32: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/32.jpg)
interface UserInterface {
public function getFormattedSalutation();
public function getName();
public function setName($name);
public function getTitle();
public function setTitle($title);
}
class User implements UserInterface { … }
![Page 33: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/33.jpg)
ABSTRACT
An abstract class is a mix between an interface and a class. It can define functionality as well as interface (in the form of abstract methods). Classes extending an abstract class must implement all of the abstract methods defined in the abstract class.
![Page 34: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/34.jpg)
abstract class User { //class
public $name; //property
public getName() { //method
echo $this->name;
}
abstract public function setName($name);
}
class Developer extends User { … }
![Page 35: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/35.jpg)
NAMESPACES
• Help create a new layer of code encapsulation
• Keep properties from colliding between areas of your code
• Only classes, interfaces, functions and constants are affected
• Anything that does not have a namespace is considered in the Global namespace (namespace = "")
![Page 36: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/36.jpg)
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
![Page 37: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/37.jpg)
• namespace myUser;
• class User { //class
• public $name; //property
• public getName() { //method
• echo $this->name;
• }
• public function setName($name);
• }
• class Developer extends \myUser\User { … }
![Page 38: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/38.jpg)
EXPLANATION COMPLETEQUESTION AND ANSWER TIME
![Page 39: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/39.jpg)
CHALLENGES
1. Change to User class to an abstract class.
2. Throw an error because your access is too restricted.
3. Extend the User class for another type of user, such as our Developer example
4. Define 2 “User” classes in one file using namespacing
![Page 40: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/40.jpg)
THANK YOU FROM ALENA HOLLIGAN
I hope you enjoyed this presentation and learned a lot in the short time we had. Please stay tuned for more and fill out the survey to help improve the training.
Contact Info:
www.sketchings.com
@sketchings
![Page 41: Demystifying Object-Oriented Programming #ssphp16](https://reader031.vdocument.in/reader031/viewer/2022013005/58757c2e1a28ab78498b628d/html5/thumbnails/41.jpg)
RESOURCES
• LeanPub: The essentials of Object Oriented PHP