SlideShare a Scribd company logo
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
alena@holligan.us
Intro to OOP with PHP
a basic look into object-oriented programming
Basic PHP to make sure your running
<html>
<body>
<?php echo “Hello world!”; ?>
</body>
</html>
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
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
Terminology
the single most important part
Terms
Class (properties, methods)
Object
Instance
Abstraction
Encapsulation
Inheritance
Terms continued
Polymorphism
Interface
Abstract
Type Hinting
vs Type Casting
Namespaces
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 getName() { //method
echo $this->name;
}
}
$user1 = new User(); //first instance of object
$user2 = new User(); //second instance of object
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.
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
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;
}
}
$user = new User();
$user->setName("Jane Smith");
$user->setTitle("Ms");
echo $user->getFormattedSalutation();
When the script is run, it will return:
Ms Jane Smith
Creating / Using the object Instance
class 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
Constructor Method & Magic Methods
$user = new User("Jane Smith","Ms");
echo $user;
When the script is run, it will return:
Ms Jane Smith
the same result as before
Creating / Using the object Instance
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
class 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();
Creating and using a child class
When run, the script returns:
Ms Jane Smith
JavasScript, HTML, CSS, PHP
Polymorphism
Polymorphism describes a pattern in object
oriented programming in which classes have
different functionality while sharing a common
interface
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
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.
abstract class User { //class
public $name; //property
public getName() { //method
echo $this->name;
}
abstract public function setName($name);
}
class Developer extends User { … }
Type Hinting
Functions are now able to force parameters to
be objects, interfaces, arrays or callable.
However, if NULL is used as the default
parameter value, it will be allowed as an
argument for any later call.
If class or interface is specified as type hint,
all its children/implementations are allowed.
class Resume {
public $user;
public function __construct(User $user) {
$this->user = $user;
}
public function formatHTML() {
$string = $this->user->getName();
...
}
}
$resume = new Resume($developer);
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 = "")
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 { … }
Explanation Complete
Question and Answer Time
Where to go from here
Resources and other things to look into
Strengthen your skills
Code Review
Pair/peer programing
Contribute to open source
Open up a personal project
Continuous learning
Participate in the community, meetups,
conferences, forums, teach
Online Training
NomadPHP.com
TeamTreehouse.com (locally on meetup)
learnhowtoprogram.com (by epicodus)
codeschool.com (great GIT intro)
freecodecamp.com, coursera.org, udemy.
com, lynda.com
Books
Design Patterns: Elements of Reusable Object-Oriented
Software - by Erich Gamma
Mastering Object Oriented PHP - by Brandon Savage
Python 3 Object Oriented Programming - by Dusty Phillips
Practical Object-Oriented Design in Ruby - by Sandi Metz
Clean Code / The Clean Coder - both by Robert Martin
The Pragmatic Programmer – by Andrew Hunt/David Thomas
Refactoring: Improving the Design of Existing Code
- by Martin Fowler
Podcasts, Videos and People
Listing: phppodcasts.com
Voices of the Elephpant
PHP Roundtable
YouTube: PHPUserGroup
NomadPHP
People: @CalEvans, @LornaJane, @adamculp
https://twitter.com/sketchings/lists/php
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
Finished
Question and Answer Time
Thank You from Alena Holligan
Help me improve:
Survey: http://goo.gl/forms/4Huh9uCSGD
Joind.in: https://joind.in/event/php-oop1
Contact Info:
www.sketchings.com
@sketchings
alena@holligan.us

More Related Content

What's hot

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
Denis Ristic
 
Introduction to PHP OOP
Introduction to PHP OOPIntroduction to PHP OOP
Introduction to PHP OOP
fakhrul hasan
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
Ravi Bhadauria
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHPMichael Peacock
 
Intro to OOP PHP and Github
Intro to OOP PHP and GithubIntro to OOP PHP and Github
Intro to OOP PHP and Github
Jo Erik San Jose
 
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 OOPWildan Maulana
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
Sayed Ahmed
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
Nyros Technologies
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
Jason Austin
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
Ramasubbu .P
 
Oops in php
Oops in phpOops in php
Oops in php
sanjay joshi
 
Php oop presentation
Php   oop presentationPhp   oop presentation
Php oop presentation
Mutinda Boniface
 
Intro to OOP and new features in PHP 5.3
Intro to OOP and new features in PHP 5.3Intro to OOP and new features in PHP 5.3
Intro to OOP and new features in PHP 5.3
Adam Culp
 
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
Toni Kolev
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
Synapseindiappsdevelopment
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 

What's hot (20)

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
 
Introduction to PHP OOP
Introduction to PHP OOPIntroduction to PHP OOP
Introduction to PHP OOP
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
 
Intro to OOP PHP and Github
Intro to OOP PHP and GithubIntro to OOP PHP and Github
Intro to OOP PHP and Github
 
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
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 
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
 
Oops in php
Oops in phpOops in php
Oops in php
 
Php oop presentation
Php   oop presentationPhp   oop presentation
Php oop presentation
 
Intro to OOP and new features in PHP 5.3
Intro to OOP and new features in PHP 5.3Intro to OOP and new features in PHP 5.3
Intro to OOP and new features in PHP 5.3
 
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
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 

Viewers also liked

Dot Net Frame Work
Dot Net Frame WorkDot Net Frame Work
Dot Net Frame WorkLiquidHub
 
Delphi developer certification study guide
Delphi developer certification study guideDelphi developer certification study guide
Delphi developer certification study guide
ANIL MAHADEV
 
DELPHI BOOT CAMP / DELPHIでビジュアル開発に挑戦しよう ◆ DAY1: Delphiで作るはじめてのアプリケーション
DELPHI BOOT CAMP / DELPHIでビジュアル開発に挑戦しよう ◆ DAY1: Delphiで作るはじめてのアプリケーションDELPHI BOOT CAMP / DELPHIでビジュアル開発に挑戦しよう ◆ DAY1: Delphiで作るはじめてのアプリケーション
DELPHI BOOT CAMP / DELPHIでビジュアル開発に挑戦しよう ◆ DAY1: Delphiで作るはじめてのアプリケーション
Kaz Aiso
 
A Simpleton's Guide to Business Intelligence in SharePoint 2010
A Simpleton's Guide to Business Intelligence in SharePoint 2010A Simpleton's Guide to Business Intelligence in SharePoint 2010
A Simpleton's Guide to Business Intelligence in SharePoint 2010Chris McNulty
 
Borland Delphi - Delphi Programming Language Guide
Borland Delphi - Delphi Programming Language GuideBorland Delphi - Delphi Programming Language Guide
Borland Delphi - Delphi Programming Language Guide
Abdelrahman Othman Helal
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
Rick Ogden
 
Delphi Certification
Delphi CertificationDelphi Certification
Delphi Certification
Andreano Lanusse
 
Delphi Innovations from Delphi 1 through Delphi XE5
Delphi Innovations from Delphi 1 through Delphi XE5Delphi Innovations from Delphi 1 through Delphi XE5
Delphi Innovations from Delphi 1 through Delphi XE5
Embarcadero Technologies
 
MySQL
MySQLMySQL
Delphi method
Delphi methodDelphi method
Delphi method
Muruli N. Tarikere
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
Bradley Holt
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 

Viewers also liked (17)

Dot Net Frame Work
Dot Net Frame WorkDot Net Frame Work
Dot Net Frame Work
 
Delphi
DelphiDelphi
Delphi
 
Delphi developer certification study guide
Delphi developer certification study guideDelphi developer certification study guide
Delphi developer certification study guide
 
DELPHI BOOT CAMP / DELPHIでビジュアル開発に挑戦しよう ◆ DAY1: Delphiで作るはじめてのアプリケーション
DELPHI BOOT CAMP / DELPHIでビジュアル開発に挑戦しよう ◆ DAY1: Delphiで作るはじめてのアプリケーションDELPHI BOOT CAMP / DELPHIでビジュアル開発に挑戦しよう ◆ DAY1: Delphiで作るはじめてのアプリケーション
DELPHI BOOT CAMP / DELPHIでビジュアル開発に挑戦しよう ◆ DAY1: Delphiで作るはじめてのアプリケーション
 
A Simpleton's Guide to Business Intelligence in SharePoint 2010
A Simpleton's Guide to Business Intelligence in SharePoint 2010A Simpleton's Guide to Business Intelligence in SharePoint 2010
A Simpleton's Guide to Business Intelligence in SharePoint 2010
 
Borland Delphi - Delphi Programming Language Guide
Borland Delphi - Delphi Programming Language GuideBorland Delphi - Delphi Programming Language Guide
Borland Delphi - Delphi Programming Language Guide
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
 
Delphi Certification
Delphi CertificationDelphi Certification
Delphi Certification
 
Delphi Innovations from Delphi 1 through Delphi XE5
Delphi Innovations from Delphi 1 through Delphi XE5Delphi Innovations from Delphi 1 through Delphi XE5
Delphi Innovations from Delphi 1 through Delphi XE5
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
MySQL
MySQLMySQL
MySQL
 
Delphi method
Delphi methodDelphi method
Delphi method
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 

Similar to OOP 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
Alena Holligan
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16
Alena 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 2017
Alena Holligan
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
Alena Holligan
 
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
Alena 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] 2017
Alena Holligan
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
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
Dhivyaa C.R
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
Demystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHPDemystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHP
Alena Holligan
 
Only oop
Only oopOnly oop
Only oop
anitarooge
 
Obect-Oriented Collaboration
Obect-Oriented CollaborationObect-Oriented Collaboration
Obect-Oriented Collaboration
Alena Holligan
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
Alena Holligan
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
Chhom Karath
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
Advanced php
Advanced phpAdvanced php
Advanced phphamfu
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
machuga
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
Iqra khalil
 

Similar to OOP in PHP (20)

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
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16
 
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 - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
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 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 Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
 
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
 
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
 
Demystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHPDemystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHP
 
Only oop
Only oopOnly oop
Only oop
 
Obect-Oriented Collaboration
Obect-Oriented CollaborationObect-Oriented Collaboration
Obect-Oriented Collaboration
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 

More from 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 .pdf
Alena Holligan
 
Environmental variables
Environmental variablesEnvironmental variables
Environmental variables
Alena Holligan
 
Dev parent
Dev parentDev parent
Dev parent
Alena Holligan
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
Alena Holligan
 
Dependency Management
Dependency ManagementDependency Management
Dependency Management
Alena Holligan
 
Experiential Project Design
Experiential Project DesignExperiential Project Design
Experiential Project Design
Alena Holligan
 
Reduce Reuse Refactor
Reduce Reuse RefactorReduce Reuse Refactor
Reduce Reuse Refactor
Alena Holligan
 
Organization Patterns: MVC
Organization Patterns: MVCOrganization Patterns: MVC
Organization Patterns: MVC
Alena Holligan
 
When & Why: Interfaces, abstract classes, traits
When & Why: Interfaces, abstract classes, traitsWhen & Why: Interfaces, abstract classes, traits
When & Why: Interfaces, abstract classes, traits
Alena Holligan
 
Object Features
Object FeaturesObject Features
Object Features
Alena Holligan
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
Let's Talk Scope
Let's Talk ScopeLet's Talk Scope
Let's Talk Scope
Alena 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 Profit
Alena Holligan
 
Environmental Variables
Environmental VariablesEnvironmental Variables
Environmental Variables
Alena Holligan
 
Learn to succeed
Learn to succeedLearn to succeed
Learn to succeed
Alena 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 #zendcon2016
Alena Holligan
 
Presentation Bulgaria PHP
Presentation Bulgaria PHPPresentation Bulgaria PHP
Presentation Bulgaria PHP
Alena Holligan
 
Presentation pnwphp
Presentation pnwphpPresentation pnwphp
Presentation pnwphp
Alena 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 2016
Alena 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 #ssphp16
Alena Holligan
 

More from 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
 

Recently uploaded

DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 

Recently uploaded (20)

DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 

OOP in PHP

  • 1. 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 alena@holligan.us
  • 2. Intro to OOP with PHP a basic look into object-oriented programming
  • 3. Basic PHP to make sure your running <html> <body> <?php echo “Hello world!”; ?> </body> </html>
  • 4. 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
  • 5. 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
  • 9. 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
  • 10. 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.
  • 11. 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
  • 12. 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
  • 13. 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.
  • 14. 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
  • 15. 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; } }
  • 16. $user = new User(); $user->setName("Jane Smith"); $user->setTitle("Ms"); echo $user->getFormattedSalutation(); When the script is run, it will return: Ms Jane Smith Creating / Using the object Instance
  • 17. class 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 Constructor Method & Magic Methods
  • 18. $user = new User("Jane Smith","Ms"); echo $user; When the script is run, it will return: Ms Jane Smith the same result as before Creating / Using the object Instance
  • 19. 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
  • 20. class 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(); Creating and using a child class
  • 21. When run, the script returns: Ms Jane Smith JavasScript, HTML, CSS, PHP
  • 22. Polymorphism Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface
  • 23. 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
  • 24. interface UserInterface { public function getFormattedSalutation(); public function getName(); public function setName($name); public function getTitle(); public function setTitle($title); } class User implements UserInterface { … }
  • 25. 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.
  • 26. abstract class User { //class public $name; //property public getName() { //method echo $this->name; } abstract public function setName($name); } class Developer extends User { … }
  • 27. Type Hinting Functions are now able to force parameters to be objects, interfaces, arrays or callable. However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call. If class or interface is specified as type hint, all its children/implementations are allowed.
  • 28. class Resume { public $user; public function __construct(User $user) { $this->user = $user; } public function formatHTML() { $string = $this->user->getName(); ... } } $resume = new Resume($developer);
  • 29. 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 = "")
  • 30. 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
  • 31. namespace myUser; class User { //class public $name; //property public getName() { //method echo $this->name; } public function setName($name); } class Developer extends myUserUser { … }
  • 33. Where to go from here Resources and other things to look into
  • 34. Strengthen your skills Code Review Pair/peer programing Contribute to open source Open up a personal project Continuous learning Participate in the community, meetups, conferences, forums, teach
  • 35. Online Training NomadPHP.com TeamTreehouse.com (locally on meetup) learnhowtoprogram.com (by epicodus) codeschool.com (great GIT intro) freecodecamp.com, coursera.org, udemy. com, lynda.com
  • 36. Books Design Patterns: Elements of Reusable Object-Oriented Software - by Erich Gamma Mastering Object Oriented PHP - by Brandon Savage Python 3 Object Oriented Programming - by Dusty Phillips Practical Object-Oriented Design in Ruby - by Sandi Metz Clean Code / The Clean Coder - both by Robert Martin The Pragmatic Programmer – by Andrew Hunt/David Thomas Refactoring: Improving the Design of Existing Code - by Martin Fowler
  • 37. Podcasts, Videos and People Listing: phppodcasts.com Voices of the Elephpant PHP Roundtable YouTube: PHPUserGroup NomadPHP People: @CalEvans, @LornaJane, @adamculp https://twitter.com/sketchings/lists/php
  • 38. 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
  • 40. Thank You from Alena Holligan Help me improve: Survey: http://goo.gl/forms/4Huh9uCSGD Joind.in: https://joind.in/event/php-oop1 Contact Info: www.sketchings.com @sketchings alena@holligan.us