01 - Intro To PHP Class
01 - Intro To PHP Class
Oriented PHP:
Introduction
Kristian Indra, Riandika Lumaris
Based on ppt slides created by Matthew Weier
O'Phinney
Start with what
you know
Constants and Variables
define(FOO, 'foo');
define(BAR, 'bar');
$value = 'bar';
switch ($value) {
case FOO:
// do something;
break;
case BAR:
// do something else
break;
}
Associative Arrays
$person = array(
'last_name' => "Weier O'Phinney",
'first_name' => "Matthew",
'occupation' => 'PHP Developer',
'gender' => 'Male',
'email' => 'matthew@example,com',
);
Functions
function mail($to, $subject, $body)
{
// ... do some stuff
// optionally return something:
return true;
}
Objects group related
subjects
Class declaration
class Person
{
}
Define class member
class Person
{
public $firstName;
public $lastName;
public $occupation;
public $gender;
public $email;
}
Types of class members
➔
Variables
Declared using a visibility operator and
normal variable naming
➔
Constants
Declared with the “const” keyword, and
no quotes
➔
Methods
Declared with the “function” keyword
Define methods
class Person
{
// ... attributes ...
public function boast()
{
return sprintf('%s %s is a %s',
$this->firstName,
$this->lastName,
$this->occupation
);
}
}
Instance variables!
Use your objects!
$matthew = new Person();
$matthew->firstName = 'Matthew';
$matthew->lastName = "Weier O'Phinney";
$matthew->email = '[email protected]';
$matthew->gender = 'male';
$matthew->occupation = 'PHP Developer';
echo $matthew->boast();
// Matthew Weier O'Phinney is a PHP Developer
Why bother?
It's good programming
➔
Scope and encapsulation
➔
Re-use
(both re-using a class by instantiating many objects,
and class extension)
➔
Type enforcement
➔
Testability and maintenance
Some words on scope
➔
Methods have access to:
➔
Whatever is passed to them
➔
Any class member variables, constants, or
methods (with respect to visibility)
➔
Use “$this->” within a class to
access member variables and methods
(unless statically declared); use “->”
when consuming an instance
➔
Use “self::” to access member
constants
What is this “visibility”?
➔
Determines scope in which a class
member may be accessed.
➔
Public: can be accessed via instances,
anywhere within the class definition, and by
any extending class.
➔
Protected: can be accessed within the
class definition, and by extending classes.
➔
Private: may only be accessed and
modified within the defining class.
Extension?
➔
Use the extends keyword:
class Matthew
extends Person
“Extends”
keyword!
Extension: override
properties
class Matthew extends Person
{
public $firstName = 'Matthew';
public $lastName = "Weier O'Phinney";
public $occupation = 'PHP Developer';
public $email = '[email protected]';
public $gender = 'male';
}
Extension: override
methods
class Matthew extends Person
{
public function boast()
{
// get parent value from method
$boast = parent::boast();
$boast .= ', and wrote this presentation';
return $boast;
}
}
Access the original method!
Extension: use the new
class
Note:
using Matthew,
not Person!
$matthew = new Matthew();
echo $matthew->boast();
// Matthew Weier O'Phinney is a PHP Developer,
// and wrote this presentation
interface Animal
{
public function makeNoise();
public function eat($food);
}
Interfaces may extend
other interfaces
interface Collection
extends Countable,
IteratorAggregate
{
}
Collection now
inherits methods
from Countable and
IteratorAggregate
Implementing an interface
class Family
extends Order
implements Collection
{
// defined by Countable
public function count() {}
// defined by IteratorAggregate
public function getIterator() {}
}
“implements” keyword;
this object follows that
contract
Abstract Classes
➔
Sometimes it's useful to create a “base”
set of functionality.
➔
At the same time, you may want to
leave certain details up to implementing
classes.
Sample Abstract Class
“abstract” keyword;
abstract class Person extending class
{ must implement
public $firstName;
public $lastName;
public $email;
public $occupation;
public $gender;