SlideShare a Scribd company logo
PHP
Absolute Basics
Object-Oriented
Programming
Daniel Kline
Web Developer
@daniel_okc
danielkline19@hotmail.com
Today we will …
 Define OOP and explain how it relates to
procedural programming
 Learn OOP basics using PHP …
 Classes vs. objects
 Class constants and internal reference
 Public vs. private scope
 Copying and cloning objects
 Create a login API
Procedural Programming is a way of programming
where we use two separate concepts to accomplish a
task: procedures and data.
Object-Oriented Programming combines these two
into one concept called objects. This allows for more
complex programming with less code.
Source: http://study.com/academy/lesson/object-oriented-programming-vs-procedural-programming.html
Class
is like a blueprint …
A
Source: http://notbutoh.com/modern-house-blueprints-fresh-with-photo-of-modern-decor-new-in-design/
Object
is like a house …
An
Source: http://www.clipartpanda.com/categories/happy-house-clipart
If we create a class called User, and we want to
alter the characteristics of the users in an
application, all we have to do is change the User
class and when we change the blueprint, the
change will be effective for all users.
Variables inside a class are called properties.
Defined as public properties, they have read/write
privileges outside of the class.
=
One blueprint … can yield many houses
Types of properties can be
• Hair color
• Shoe size
• How tall someone is
• Hobby
• Musical instrument they play
Source: http://www.popsugar.com/beauty/Sand-Art-Rainbow-Hair-Color-37681502#photo-37681502
Source: http://www.zazzle.com/height+jokes+clothing
Example 1:
Create a class that includes email and password
properties and methods for indicating that a user has
logged in and logged out.
Procedure
1) Create a class named User
2) In an appropriately named file named user.php
3) Add this code to the file …
<?php
class User {
public $email;
public $password;
}
Let’s create login and logout functions for our user
class by adding code to our user class file …
public function login() {
return ‘Logging in …’;
}
public function logout() {
return ‘Logging out …’;
}
Create an index.php file from which we will call
our class.
1) Need to include the user class file
2) Create an object named $joost
<?php
require ‘user.php’;
$joost = new User();
The user $joost how has an email and a password.
S/he can login and logout. Let’s test the code.
1) Add to index.php
var_dump($joost);
Add to index.php …
Let’s take a look at our code for index.php …
<?php
require ‘user.php’;
$joost = new User();
var_dump($joost)
Let’s run the code and take a look at the output …
object(User)#1 (2) { ["email"]=> NULL
["password"]=> NULL }
Output shows that we have an object named User
defined and there are two properties email and
password. Since there was not value assigned they
have a value of NULL.
object_name->property_name
This is the assignment operator which means that we
want to access a property (on the right side) from the
object (on the left).
$person->haircolor
$person->shoesize
Examples …
Assign an email address to a variable using procedural
programming …
$mail = ‘the@email.com’;
If we wanted to assign an email to the email property of
the $joost object, we would do the following …
$joost->email = ‘joost@tutsplus.com’;
Add to the code index.php …
var_dump($joost);
Resulting output …
object(User)#1 (2) { ["email"]=> string(18)
"joost@tutsplus.com" ["password"]=> NULL }
Set a password to random gibberish for $joost …
$joost->password = ‘ahj%jk@#’;
Create another user named $nick …
$nick = new User();
$nick->email = 'nick@tutsplus.com';
$nick->password = 'hie@kwi^';
Add to index.php …
var_dump($nick->login());
Object Oriented Programming Basics with PHP
Resulting output …
string(14) "Logging in ..."
Constants and Internal Reference
Constant
• Numerical value identifier
• Value cannot be changed
• Must start with letter or underscore (no ‘$’ sign)
• Global across entire script (class)
• No name collisions if used another constant with
same name is used outside of class
• Recommended to use uppercase letters for easy
identification
Example 2:
Define the minimum number of characters for a
password. If the password is too short, we want to
throw an exception.
Assume we have a constant with the name
MINCHARS, we access the constant with the
following syntax including the keyword self:
self::MINCHARS
The double colon ‘::’ is the scope resolution
operator
We could define the constant in the index.php file as
such:
define(MINCHARS,8);
const MINCHARS = 8;
Instead, we will make the definition in the user.php file:
Create a method in user.php to set the password:
public function setPassword($string) {
if (strlen($string) < self::MINCHARS) {
throw new Exception('the password
should be at least ' . self::MINCHARS .
' characters long.');
}
}
We need to update index.php in order to make this
work:
Remove
$joost->email = 'joost@tutsplus.com';
$joost->password = 'ahj%jk@#';
$nick = new User();
$nick->email = 'nick@tutsplus.com';
$nick->password = 'hie@kwi^';
var_dump($nick->login());
Replace with
$joost->setPassword('asdf');
Let’s see the output …
Fatal error: Uncaught exception 'Exception'
with message 'the password should be at
least 8 characters long.’
If an error message is thrown due to the scope
resolution operator ‘::,’ a message “Paamayim
Nekudotayim” will appear. This is Hebrew meaning
double colon.
Internal reference. The expression:
$this->password
states that we want to access the password object
within the current class. This is the reason for the
$this keyword.
This part can be confusing …
self reference constants
this reference properties and methods
If password passes validation, we want to set it to a
hash using a cryptographic hash function. Here is the
syntax:
$this->password = hash(‘sha256’, $string);
Add the hash to the setPassword method in the User
class …
$this->password = hash(‘sha256’, $string);
In index.php, assign a long password and var_dump
$joost->setPassword('ah@94^hjq’);
var_dump($joost);
Object Oriented Programming Basics with PHP
Object Oriented Programming Basics with PHP
Output …
object(User)#1 (2) { ["email"]=>
NULL ["password"]=> string(64)
"4b875365586d5de0e6e4cfd04586b41f560d
9f95ea5faba2278eeaa5fc620205" }
Result of the hashed password is …
"4b875365586d5de0e6e4cfd04586b41f560d
9f95ea5faba2278eeaa5fc620205"
We use Visibility markers to indicate the scope of a
property or method. Examples are public, private or
protected. We will use the public and private markers in
today’s presentation.
Public scope allows the rest of the application to see
what we are doing while Private scope does not.
If data is to be secured (i.e., for use as a password), we
would want private scope and we could reuse it in the
user class and outsiders could not see it.
Change the scope of the password property to private in
the User class…
Change public $password;
To private $password;
Dump password for $joost …
var_dump($joost->password);
Output …
Fatal error: Cannot access private property
User::$password
Example 3:
Let’s change the password directly from the PHP by
modifying index.php code …
Remove: var_dump($joost->password);
Change: $joost->setPassword(‘asdfuioe’);
To: $joost->password = ‘asdfuioe’;
Fatal error: Cannot access private property
User::$password
Same message as before
Output …
One of the goals for OOP is to encapsulate the data
within our objects.
• An object should be like a black box
• The outside world should not be aware of its internal
workings
• We simply feed parameters into the object and get a
response in return and only then can our object act
independently from the rest of the application
• This way we can change the entire internals for an
object and it would not break the rest of our
application
height
email
password
hair_color
shoe_size
Class with 5 properties,
one instance of the object …
height
email
password
hair_color
shoe_size
$joost =
$mike = height
email
password
hair_color
shoe_size
Two objects from the same
class …
• The outside world has nothing to do with where
the value is stored or if it’s filtered or validated
in any way
• It just needs a way to get and set that property’s
value without direct access to the property itself
• We already have a perfect example for that in
our user class
• If somebody outside of our user class weren’t
able to change the password for the user
directly, s/he could easily make this an unsafe
password like ‘1234’
• We created the setPassword method to prevent the
password from only being stored as plain text
• If we want to change the password from the
outside, we have to use the following method we
already created in the User class
• We have to make sure the password is properly
validated and hashed before it is stored
Example 4:
Create another method to get the value of the user’s
password. This will be an API to set and get a user’s
password.
Add code to the User class…
public function getPassword() {
return $this->password;
}
Since we know the password is properly validated and
hashed, we can simply return with the statement
return $this->password;
Let’s see it in action …
Add code to index.php …
$joost->setPassword(‘ah@94^hjq’);
var_dump($joost->getPassword());
Result …
string(64)
"4b875365586d5de0e6e4cfd04586b41
f560d9f95ea5faba2278eeaa5fc620205”
Example 5:
Let’s do the same for email …
Change the visibility marker for $email to
private in User class …
private $email;
Create a public method with validation …
public function setEmail($string) {
if (! filter_var($string,
FILTER_VALIDATE_EMAIL)) {
throw new Exception('Please provide
a valid email.');
}
$this->email = $string;
}
public function getEmail() {
return $this->email;
}
Just like with properties, we can also set the visibility
scope of a method to private. We would do this for
methods that are for internal use only. We could abstract
password validation away from the setPassword method.
To do this create a validatePassword() method …
private function validatePassword($string) {
return strlen($string) < self::MINCHARS ?
false : true;
}
return false if fail
return true if true
Change password to ‘1234’ verify output …
Result Fatal error: Uncaught exception
'Exception’ with message 'the password
should be at least 8 characters long.'
Change to a longer password …
Result
string(64) "b86b2373aa5a777eb535b2Ea25d
bce8bda42844cad7bf418edf69d9ac1fc82e2"
We can leave off the visibility marker for a method
meaning that the method would be accessible publicly
(default) same as if we prepended it with the word public
Sometimes it may be useful to create an object from a
class and then copy it a couple of times. In this case, it is
good to know how PHP deals with copying.
By default, objects are copied by reference not by value.
what does this mean exactly?
Example 6:
let’s copy an object by reference …
We already have the user named $joost
create a brother for $joost named $mike
index.php
Remove from code …
$joost->setEmail('joost@tutsplus.php');
Add to code …
$mike = $joost;
var_dump($mike);
var_dump($joost);
Output …
object(User)#1 (2) { ["email":"User":private]=>
string(18) "joost@tutsplus.com"
["password":"User":private]=> string(64)
"0554a5df02ee12f1ae36a51caaef34a31deb9458a4
8b629da554a2b322466f4a" }
object(User)#1 (2) { ["email":"User":private]=>
string(18) "joost@tutsplus.com"
["password":"User":private]=> string(64)
"0554a5df02ee12f1ae36a51caaef34a31deb9458a4
8b629da554a2b322466f4a" }
Now see what happens when we change $mike’s
email to something different than that for $joost
add to code in index.php …
$mike->setEmail('mike@tutsplus.php');
object(User)#1 (2) { ["email":"User":private]=>
string(17) "mike@tutsplus.php"
["password":"User":private]=> string(64)
"b86b2373aa5a777eb535b2ea25dbce8bda42844ca
d7bf418edf69d9ac1fc82e2" }
object(User)#1 (2) { ["email":"User":private]=>
string(17) "mike@tutsplus.php"
["password":"User":private]=> string(64)
"b86b2373aa5a777eb535b2ea25dbce8bda42844ca
d7bf418edf69d9ac1fc82e2" }
Result …
Now change $joost’s email to something else.
Add this to the code, without removing the original
email assignment for $joost …
$joost->setEmail(‘joost@tutsplus.php’);
Both emails have changed to mike@tutsplus.com
object(User)#1 (2) { ["email":"User":private]=>
string(18) "joost@tutsplus.php"
["password":"User":private]=> string(64)
"b86b2373aa5a777eb535b2ea25dbce8bda42844ca
d7bf418edf69d9ac1fc82e2" }
object(User)#1 (2) { ["email":"User":private]=>
string(18) "joost@tutsplus.php"
["password":"User":private]=> string(64)
"b86b2373aa5a777eb535b2ea25dbce8bda42844ca
d7bf418edf69d9ac1fc82e2" }
Result …
Example 7:
What if we wanted to create a copy from a user but
only want to change the email address for that specific
copy?
In this case we need to copy the object by value.
Do this by using the word clone. This will make
$joost and $mike two separate objects
replace
$mike = $joost;
with
$mike = clone $joost;
$mike->setEmail('mike@tutsplus.com');
Object Oriented Programming Basics with PHP
Object Oriented Programming Basics with PHP
Object Oriented Programming Basics with PHP
object(User)#1 (2) { ["email":"User":private]=>
string(18) "joost@tutsplus.php"
["password":"User":private]=> string(64)
"b86b2373aa5a777eb535b2ea25dbce8bda42844ca
d7bf418edf69d9ac1fc82e2" }
object(User)#2 (2) { ["email":"User":private]=>
string(17) "mike@tutsplus.php"
["password":"User":private]=> string(64)
"b86b2373aa5a777eb535b2ea25dbce8bda42844ca
d7bf418edf69d9ac1fc82e2" }
Result …
Credits
• All code samples from PHP Object Oriented
Programming Fundamentals with Joost Van Veen;
evantotuts+ (https://tutsplus.com), March 31,
2014
Daniel Kline
Web Developer
@daniel_okc
danielkline19@hotmail.com
PHP
Absolute Basics
Object-Oriented
Programming
Ad

Recommended

Fnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
Fnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
fntsofttech
 
Object Oriented PHP5
Object Oriented PHP5
Jason Austin
 
Php Security By Mugdha And Anish
Php Security By Mugdha And Anish
OSSCube
 
Doctrator Symfony Live 2011 San Francisco
Doctrator Symfony Live 2011 San Francisco
pablodip
 
Practice exam php
Practice exam php
Yesenia Sánchez Sosa
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in php
Synapseindiappsdevelopment
 
Task 2
Task 2
EdiPHP
 
PHP Technical Questions
PHP Technical Questions
Pankaj Jha
 
Who killed object oriented design?
Who killed object oriented design?
Amir Barylko
 
Functions in PHP
Functions in PHP
Vineet Kumar Saini
 
Os Leonard
Os Leonard
oscon2007
 
Oops concept in php
Oops concept in php
selvabalaji k
 
Prototype Utility Methods(1)
Prototype Utility Methods(1)
mussawir20
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
Lab#1 - Front End Development
Lab#1 - Front End Development
Walid Ashraf
 
$$$ +$$+$$ _+_$+$$_$+$$$_+$$_$
$$$ +$$+$$ _+_$+$$_$+$$$_+$$_$
UltraUploader
 
Everything about Object Oriented Programming
Everything about Object Oriented Programming
Abdul Rahman Sherzad
 
Learning from other's mistakes: Data-driven code analysis
Learning from other's mistakes: Data-driven code analysis
Andreas Dewes
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
Andreas Dewes
 
Entities in drupal 7
Entities in drupal 7
Zsolt Tasnadi
 
Jquery presentation
Jquery presentation
guest5d87aa6
 
My app is secure... I think
My app is secure... I think
Wim Godden
 
Cfphp Zce 01 Basics
Cfphp Zce 01 Basics
Michael Girouard
 
Smarty
Smarty
Mohammad Bagher Adib Behrooz
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.com
JD Leonard
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
Java Persistence API
Java Persistence API
Carol McDonald
 
Java
Java
Aashish Jain
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
Object Oriented Programming in PHP
Object Oriented Programming in PHP
wahidullah mudaser
 

More Related Content

What's hot (20)

Who killed object oriented design?
Who killed object oriented design?
Amir Barylko
 
Functions in PHP
Functions in PHP
Vineet Kumar Saini
 
Os Leonard
Os Leonard
oscon2007
 
Oops concept in php
Oops concept in php
selvabalaji k
 
Prototype Utility Methods(1)
Prototype Utility Methods(1)
mussawir20
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
Lab#1 - Front End Development
Lab#1 - Front End Development
Walid Ashraf
 
$$$ +$$+$$ _+_$+$$_$+$$$_+$$_$
$$$ +$$+$$ _+_$+$$_$+$$$_+$$_$
UltraUploader
 
Everything about Object Oriented Programming
Everything about Object Oriented Programming
Abdul Rahman Sherzad
 
Learning from other's mistakes: Data-driven code analysis
Learning from other's mistakes: Data-driven code analysis
Andreas Dewes
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
Andreas Dewes
 
Entities in drupal 7
Entities in drupal 7
Zsolt Tasnadi
 
Jquery presentation
Jquery presentation
guest5d87aa6
 
My app is secure... I think
My app is secure... I think
Wim Godden
 
Cfphp Zce 01 Basics
Cfphp Zce 01 Basics
Michael Girouard
 
Smarty
Smarty
Mohammad Bagher Adib Behrooz
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.com
JD Leonard
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
Java Persistence API
Java Persistence API
Carol McDonald
 
Java
Java
Aashish Jain
 
Who killed object oriented design?
Who killed object oriented design?
Amir Barylko
 
Prototype Utility Methods(1)
Prototype Utility Methods(1)
mussawir20
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
Lab#1 - Front End Development
Lab#1 - Front End Development
Walid Ashraf
 
$$$ +$$+$$ _+_$+$$_$+$$$_+$$_$
$$$ +$$+$$ _+_$+$$_$+$$$_+$$_$
UltraUploader
 
Everything about Object Oriented Programming
Everything about Object Oriented Programming
Abdul Rahman Sherzad
 
Learning from other's mistakes: Data-driven code analysis
Learning from other's mistakes: Data-driven code analysis
Andreas Dewes
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
Andreas Dewes
 
Entities in drupal 7
Entities in drupal 7
Zsolt Tasnadi
 
Jquery presentation
Jquery presentation
guest5d87aa6
 
My app is secure... I think
My app is secure... I think
Wim Godden
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.com
JD Leonard
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
Ankit Rastogi
 

Similar to Object Oriented Programming Basics with PHP (20)

Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
Object Oriented Programming in PHP
Object Oriented Programming in PHP
wahidullah mudaser
 
Ch8(oop)
Ch8(oop)
Chhom Karath
 
Oops in php
Oops in php
Gourishankar R Pujar
 
Introduction to OOP with PHP
Introduction to OOP with PHP
Michael Peacock
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
OOP in PHP.pptx
OOP in PHP.pptx
switipatel4
 
Object Features
Object Features
Alena Holligan
 
Php oop (1)
Php oop (1)
Sudip Simkhada
 
Introduction to PHP and MySql basics.pptx
Introduction to PHP and MySql basics.pptx
PriyankaKupneshi
 
OOP in PHP
OOP in PHP
Alena Holligan
 
Php object orientation and classes
Php object orientation and classes
Kumar
 
Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdf
GammingWorld2
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
Rick Ogden
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptx
Atikur Rahman
 
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
ajayparmeshwarmahaja
 
tutorial54
tutorial54
tutorialsruby
 
tutorial54
tutorial54
tutorialsruby
 
Advanced php
Advanced php
hamfu
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
Object Oriented Programming in PHP
Object Oriented Programming in PHP
wahidullah mudaser
 
Introduction to OOP with PHP
Introduction to OOP with PHP
Michael Peacock
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
Introduction to PHP and MySql basics.pptx
Introduction to PHP and MySql basics.pptx
PriyankaKupneshi
 
Php object orientation and classes
Php object orientation and classes
Kumar
 
Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdf
GammingWorld2
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
Rick Ogden
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptx
Atikur Rahman
 
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
ajayparmeshwarmahaja
 
Advanced php
Advanced php
hamfu
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
Ad

Recently uploaded (20)

Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Ad

Object Oriented Programming Basics with PHP

  • 3. Today we will …  Define OOP and explain how it relates to procedural programming  Learn OOP basics using PHP …  Classes vs. objects  Class constants and internal reference  Public vs. private scope  Copying and cloning objects  Create a login API
  • 4. Procedural Programming is a way of programming where we use two separate concepts to accomplish a task: procedures and data. Object-Oriented Programming combines these two into one concept called objects. This allows for more complex programming with less code. Source: http://study.com/academy/lesson/object-oriented-programming-vs-procedural-programming.html
  • 5. Class is like a blueprint … A Source: http://notbutoh.com/modern-house-blueprints-fresh-with-photo-of-modern-decor-new-in-design/
  • 6. Object is like a house … An Source: http://www.clipartpanda.com/categories/happy-house-clipart
  • 7. If we create a class called User, and we want to alter the characteristics of the users in an application, all we have to do is change the User class and when we change the blueprint, the change will be effective for all users. Variables inside a class are called properties. Defined as public properties, they have read/write privileges outside of the class.
  • 8. = One blueprint … can yield many houses
  • 9. Types of properties can be • Hair color • Shoe size • How tall someone is • Hobby • Musical instrument they play
  • 12. Example 1: Create a class that includes email and password properties and methods for indicating that a user has logged in and logged out.
  • 13. Procedure 1) Create a class named User 2) In an appropriately named file named user.php 3) Add this code to the file … <?php class User { public $email; public $password; }
  • 14. Let’s create login and logout functions for our user class by adding code to our user class file … public function login() { return ‘Logging in …’; } public function logout() { return ‘Logging out …’; }
  • 15. Create an index.php file from which we will call our class. 1) Need to include the user class file 2) Create an object named $joost <?php require ‘user.php’; $joost = new User();
  • 16. The user $joost how has an email and a password. S/he can login and logout. Let’s test the code. 1) Add to index.php var_dump($joost);
  • 18. Let’s take a look at our code for index.php … <?php require ‘user.php’; $joost = new User(); var_dump($joost)
  • 19. Let’s run the code and take a look at the output … object(User)#1 (2) { ["email"]=> NULL ["password"]=> NULL } Output shows that we have an object named User defined and there are two properties email and password. Since there was not value assigned they have a value of NULL.
  • 20. object_name->property_name This is the assignment operator which means that we want to access a property (on the right side) from the object (on the left).
  • 22. Assign an email address to a variable using procedural programming … $mail = ‘[email protected]’; If we wanted to assign an email to the email property of the $joost object, we would do the following … $joost->email = ‘[email protected]’;
  • 23. Add to the code index.php … var_dump($joost); Resulting output … object(User)#1 (2) { ["email"]=> string(18) "[email protected]" ["password"]=> NULL }
  • 24. Set a password to random gibberish for $joost … $joost->password = ‘ahj%jk@#’; Create another user named $nick … $nick = new User(); $nick->email = '[email protected]'; $nick->password = 'hie@kwi^'; Add to index.php … var_dump($nick->login());
  • 27. Constants and Internal Reference Constant • Numerical value identifier • Value cannot be changed • Must start with letter or underscore (no ‘$’ sign) • Global across entire script (class) • No name collisions if used another constant with same name is used outside of class • Recommended to use uppercase letters for easy identification
  • 28. Example 2: Define the minimum number of characters for a password. If the password is too short, we want to throw an exception. Assume we have a constant with the name MINCHARS, we access the constant with the following syntax including the keyword self: self::MINCHARS The double colon ‘::’ is the scope resolution operator
  • 29. We could define the constant in the index.php file as such: define(MINCHARS,8); const MINCHARS = 8; Instead, we will make the definition in the user.php file:
  • 30. Create a method in user.php to set the password: public function setPassword($string) { if (strlen($string) < self::MINCHARS) { throw new Exception('the password should be at least ' . self::MINCHARS . ' characters long.'); } }
  • 31. We need to update index.php in order to make this work: Remove $joost->email = '[email protected]'; $joost->password = 'ahj%jk@#'; $nick = new User(); $nick->email = '[email protected]'; $nick->password = 'hie@kwi^'; var_dump($nick->login()); Replace with $joost->setPassword('asdf');
  • 32. Let’s see the output … Fatal error: Uncaught exception 'Exception' with message 'the password should be at least 8 characters long.’ If an error message is thrown due to the scope resolution operator ‘::,’ a message “Paamayim Nekudotayim” will appear. This is Hebrew meaning double colon.
  • 33. Internal reference. The expression: $this->password states that we want to access the password object within the current class. This is the reason for the $this keyword.
  • 34. This part can be confusing … self reference constants this reference properties and methods If password passes validation, we want to set it to a hash using a cryptographic hash function. Here is the syntax: $this->password = hash(‘sha256’, $string);
  • 35. Add the hash to the setPassword method in the User class … $this->password = hash(‘sha256’, $string); In index.php, assign a long password and var_dump $joost->setPassword('ah@94^hjq’); var_dump($joost);
  • 38. Output … object(User)#1 (2) { ["email"]=> NULL ["password"]=> string(64) "4b875365586d5de0e6e4cfd04586b41f560d 9f95ea5faba2278eeaa5fc620205" } Result of the hashed password is … "4b875365586d5de0e6e4cfd04586b41f560d 9f95ea5faba2278eeaa5fc620205"
  • 39. We use Visibility markers to indicate the scope of a property or method. Examples are public, private or protected. We will use the public and private markers in today’s presentation. Public scope allows the rest of the application to see what we are doing while Private scope does not. If data is to be secured (i.e., for use as a password), we would want private scope and we could reuse it in the user class and outsiders could not see it.
  • 40. Change the scope of the password property to private in the User class… Change public $password; To private $password; Dump password for $joost … var_dump($joost->password); Output … Fatal error: Cannot access private property User::$password
  • 41. Example 3: Let’s change the password directly from the PHP by modifying index.php code … Remove: var_dump($joost->password); Change: $joost->setPassword(‘asdfuioe’); To: $joost->password = ‘asdfuioe’; Fatal error: Cannot access private property User::$password Same message as before Output …
  • 42. One of the goals for OOP is to encapsulate the data within our objects. • An object should be like a black box • The outside world should not be aware of its internal workings • We simply feed parameters into the object and get a response in return and only then can our object act independently from the rest of the application • This way we can change the entire internals for an object and it would not break the rest of our application
  • 43. height email password hair_color shoe_size Class with 5 properties, one instance of the object …
  • 44. height email password hair_color shoe_size $joost = $mike = height email password hair_color shoe_size Two objects from the same class …
  • 45. • The outside world has nothing to do with where the value is stored or if it’s filtered or validated in any way • It just needs a way to get and set that property’s value without direct access to the property itself • We already have a perfect example for that in our user class • If somebody outside of our user class weren’t able to change the password for the user directly, s/he could easily make this an unsafe password like ‘1234’
  • 46. • We created the setPassword method to prevent the password from only being stored as plain text • If we want to change the password from the outside, we have to use the following method we already created in the User class • We have to make sure the password is properly validated and hashed before it is stored
  • 47. Example 4: Create another method to get the value of the user’s password. This will be an API to set and get a user’s password. Add code to the User class… public function getPassword() { return $this->password; } Since we know the password is properly validated and hashed, we can simply return with the statement return $this->password;
  • 48. Let’s see it in action … Add code to index.php … $joost->setPassword(‘ah@94^hjq’); var_dump($joost->getPassword()); Result … string(64) "4b875365586d5de0e6e4cfd04586b41 f560d9f95ea5faba2278eeaa5fc620205”
  • 49. Example 5: Let’s do the same for email … Change the visibility marker for $email to private in User class … private $email;
  • 50. Create a public method with validation … public function setEmail($string) { if (! filter_var($string, FILTER_VALIDATE_EMAIL)) { throw new Exception('Please provide a valid email.'); } $this->email = $string; } public function getEmail() { return $this->email; }
  • 51. Just like with properties, we can also set the visibility scope of a method to private. We would do this for methods that are for internal use only. We could abstract password validation away from the setPassword method. To do this create a validatePassword() method … private function validatePassword($string) { return strlen($string) < self::MINCHARS ? false : true; } return false if fail return true if true
  • 52. Change password to ‘1234’ verify output … Result Fatal error: Uncaught exception 'Exception’ with message 'the password should be at least 8 characters long.' Change to a longer password … Result string(64) "b86b2373aa5a777eb535b2Ea25d bce8bda42844cad7bf418edf69d9ac1fc82e2"
  • 53. We can leave off the visibility marker for a method meaning that the method would be accessible publicly (default) same as if we prepended it with the word public Sometimes it may be useful to create an object from a class and then copy it a couple of times. In this case, it is good to know how PHP deals with copying. By default, objects are copied by reference not by value. what does this mean exactly?
  • 54. Example 6: let’s copy an object by reference … We already have the user named $joost create a brother for $joost named $mike index.php Remove from code … $joost->setEmail('[email protected]'); Add to code … $mike = $joost; var_dump($mike); var_dump($joost);
  • 55. Output … object(User)#1 (2) { ["email":"User":private]=> string(18) "[email protected]" ["password":"User":private]=> string(64) "0554a5df02ee12f1ae36a51caaef34a31deb9458a4 8b629da554a2b322466f4a" } object(User)#1 (2) { ["email":"User":private]=> string(18) "[email protected]" ["password":"User":private]=> string(64) "0554a5df02ee12f1ae36a51caaef34a31deb9458a4 8b629da554a2b322466f4a" }
  • 56. Now see what happens when we change $mike’s email to something different than that for $joost add to code in index.php … $mike->setEmail('[email protected]');
  • 57. object(User)#1 (2) { ["email":"User":private]=> string(17) "[email protected]" ["password":"User":private]=> string(64) "b86b2373aa5a777eb535b2ea25dbce8bda42844ca d7bf418edf69d9ac1fc82e2" } object(User)#1 (2) { ["email":"User":private]=> string(17) "[email protected]" ["password":"User":private]=> string(64) "b86b2373aa5a777eb535b2ea25dbce8bda42844ca d7bf418edf69d9ac1fc82e2" } Result …
  • 58. Now change $joost’s email to something else. Add this to the code, without removing the original email assignment for $joost … $joost->setEmail(‘[email protected]’); Both emails have changed to [email protected]
  • 59. object(User)#1 (2) { ["email":"User":private]=> string(18) "[email protected]" ["password":"User":private]=> string(64) "b86b2373aa5a777eb535b2ea25dbce8bda42844ca d7bf418edf69d9ac1fc82e2" } object(User)#1 (2) { ["email":"User":private]=> string(18) "[email protected]" ["password":"User":private]=> string(64) "b86b2373aa5a777eb535b2ea25dbce8bda42844ca d7bf418edf69d9ac1fc82e2" } Result …
  • 60. Example 7: What if we wanted to create a copy from a user but only want to change the email address for that specific copy? In this case we need to copy the object by value. Do this by using the word clone. This will make $joost and $mike two separate objects replace $mike = $joost; with $mike = clone $joost; $mike->setEmail('[email protected]');
  • 64. object(User)#1 (2) { ["email":"User":private]=> string(18) "[email protected]" ["password":"User":private]=> string(64) "b86b2373aa5a777eb535b2ea25dbce8bda42844ca d7bf418edf69d9ac1fc82e2" } object(User)#2 (2) { ["email":"User":private]=> string(17) "[email protected]" ["password":"User":private]=> string(64) "b86b2373aa5a777eb535b2ea25dbce8bda42844ca d7bf418edf69d9ac1fc82e2" } Result …
  • 65. Credits • All code samples from PHP Object Oriented Programming Fundamentals with Joost Van Veen; evantotuts+ (https://tutsplus.com), March 31, 2014