0% found this document useful (0 votes)
2 views31 pages

01 - Intro To PHP Class

The document provides an introduction to Object-Oriented PHP, covering key concepts such as constants, variables, associative arrays, classes, methods, and visibility. It explains the importance of encapsulation, reusability, and type enforcement in programming. Additionally, it discusses advanced topics like interfaces, abstract classes, magic methods, and type validation.

Uploaded by

hendy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

01 - Intro To PHP Class

The document provides an introduction to Object-Oriented PHP, covering key concepts such as constants, variables, associative arrays, classes, methods, and visibility. It explains the importance of encapsulation, reusability, and type enforcement in programming. Additionally, it discusses advanced topics like interfaces, abstract classes, magic methods, and type validation.

Uploaded by

hendy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Object

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

boast() now says


something new!
Abstraction

A formal way of defining extension
points

Two types:

Interfaces

Abstract Classes
Interfaces

Defines a public contract for classes that
implement it

Ensures consuming classes will always
have specific methods available
A sample interface
“Interface”
keyword!

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;

public function speak() {


/* implementation in here */
}
// let extension define this, though:
abstract public function eat($food);
}
Abstract classes can also
implement interfaces

abstract class Family


extends Order
implements Collection
{
}
Magic methods

Always prefixed by “__” (double
underscore)

Some tie into various object states
(initialization, destruction, cloning)
Object initialization
Magic method:
constructor

public function __construct(


$firstName, $lastName
) {
$this->firstName = $firstName;
$this->lastName = $lastName;
}
Type validation

PHP allows you to typehint on any class,
abstract class, or interface name.
if (!$matthew instanceof Person) {
echo "Imposter!";
}
Resources

http://thinkvitamin.com/author/
lornajanemitchell/

http://php.net/oop

http://php.net/spl

You might also like