Object-Oriented PHP: Essential Constructs
Essential Constructs
Daryl K Wood
@datashuttle |
www.datashuttle.netwww.linkedin.com/in/datashuttle
Course Overview
Introduction
Knowledge prerequisite
Installed software requirement
Goals for this module
Knowledge Prerequisites
Basic PHP language data types
Operators
Control statements (if, switch)
Looping (foreach, for, while, do-while)
Functions
Scope
Software Prerequisites
PHP installed as either a web server
module, or as a command line version
A web server if using PHP as a server
module
An integrated development environment
(IDE)
Object-Oriented PHP Module 1
The Class Construct
$fields = ['username', 'password', 'hairColor'];
//Use of a crop rotation extension that defines a fields
variable
$fields = ['soy_beans', 'corn', 'potatoes'];
A Naming Collision
A variable in your application can be redefined later by use of another library or
extension.
Best Practice: Encapsulate your code in a protected scope called a class.
The Class Construct
What is a class
Class encapsulation and scope
Class properties (variables)
Class methods (functions)
Class constants
Best practices
What Is a Class?
A container of information
Provides access control to its
contents
Class Encapsulation and Scope
A graphical representation of a
class container (UML)
Contains class member properties
and methods
Encapsulation
Encapsulation is the packing of data and functions
into a single component.
It allows selective hiding of properties and methods
in an object by building an impenetrable wall to
protect the code from accidental corruption.
Source: wikipedia.org
class <ClassName> {}
The Class Construct Syntax
This is the basic class construct. In this simplest form, it encapsulates.
Best Practice: Name starts with upper case letter, followed by camel-cased characters.
The Class Construct Summary
The problem with naming collisions
Encapsulation and scoping
Introduction of the class construct
Best practices
IDE place holder
Class Properties
What is a class member property
Property visibility and declaration
Setting default values
Class Properties (variables)
Class properties belong to the class
Contain changeable (malleable)
values
Other names: “variables”,
“attributes” or “fields”
Property Visibility
Public Protected Private
class Form{
protected $elements = [];
protected $name;
public $valid = false;
}
Class Properties
Prefixing the property declaration with one of three visibilities: public, protected or
private.
Setting default values.
Class Properties Summary
Class properties
Visibility
Assigning default values
Best practices
Slide Deck Clip Break
Class Methods
What is a class member method
Method visibility and declaration
Method scope
Class Methods (functions)
Class methods belong to the class
Class methods have visibility
Other name: “class function”,
Can take parameters
Has a scope
Method Visibility
Public Protected Private
A Class Method
Class Methods Summary
Class methods
Visibility
Scope
Best practices
IDE place holder
Class Constants
What is a class member constant
Constant visibility and declaration
Assigning default values
Constant scope
Best practices
Class Constants
Class constants belong to the class
Are always publicly visible
Do not use a dollar sign ($)
Class Form {
const <CONSTANT_NAME> = 'Some name here';
}
The Class Constant Syntax
This basic syntax assigns a hard-coded value to the constant name.
Use constants as a single edit point for multiple class references.
Best Practice: Name with upper case letters.
Class Constants Summary
Class constants
Visibility as it relates to constants
Defining a default value
Scope
Best practices
IDE place holder
The Form Class
What will the Form class do
Features of the Form class
Look over the Form class as built
A Blueprint to Build Something
Like an HTML Form
Features of The Form Class
Provide for object-oriented form creation
Built as a single concern
Integrate various HTML form inputs
Provide form input validation
Integrate input security
IDE place holder
Form Class Summary
Built the Form class foundation
Form class usage
Form class code review