Top Banner
OBJECT ORIENTED PROGRAMMING IN PHP HENRY OSBORNE
16

OOP in PHP

Jan 15, 2015

Download

Education

Henry Osborne

Object orientation is probably the area that has been subject to the most significant and far-reaching changes with the advent of PHP 5. Rather than making things incompatible with previous versions of the language, however, they enhance PHP 4’s meagre OOP offerings and make PHP 5 a fully functional object-oriented language.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OOP in PHP

OBJECT ORIENTED

PROGRAMMING IN PHP

HENRY OSBORNE

Page 2: OOP in PHP

FUNDAMENTALS

•Classes

•Encapsulation

•Objects

Page 3: OOP in PHP

CLASS DECLARATION

The basic declaration of a class is very simple:

class myClass {

// Class contents go here

}

Page 4: OOP in PHP

OBJECT INSTANTIATION

This is done by using the new construct:

$myClassInstance = new myClass();

An object is always passed by reference rather than by value.

$myClassInstance = new myClass();

$copyInstance = $myClassInstance();

Page 5: OOP in PHP

CLASS INHERITANCEAllows a class to extend another class, essentially adding new methods and properties, as well as overriding existing ones as needed.class a {

function test(){

echo "a::test called";}function func(){

echo "a::func called";}

}class b extends a {

function test(){

echo "b::test called";}

}

class c extends b {function test(){

parent::test();}

}class d extends c {

function test(){

b::test();}

}$a = new a();$b = new b();$c = new c();$d = new d();

Page 6: OOP in PHP

METHODS AND PROPERTIESMethods are declared just like traditional functions:

class myClass {

function myFunction() {

echo "You called myClass::myFunction";

}

}

From outside the scope of a class, its methods are called using the indirection operator ->:

$obj = new myClass();

$obj->myFunction();

Page 7: OOP in PHP

METHODS AND PROPERTIESclass myClass {

function myFunction($data) {

echo "The value is $data";

}

function callMyFunction($data) {

// Call myFunction()

$this->myFunction($data);

}

}

$obj = new myClass();

$obj->callMyFunction(123);

Page 8: OOP in PHP

CONSTRUCTORSclass foo {

function __construct()

{

echo __METHOD__;

}

function foo()

{

// PHP 4 style constructor

}

}

new foo();

Page 9: OOP in PHP

DESTRUCTORSclass foo {

function __construct()

{

echo __METHOD__ . PHP_EOL;

}

function __destruct()

{

echo __METHOD__;

}

}

new foo();

This code will display:

foo::__construct

foo::__destruct

Page 10: OOP in PHP

VISIBILITYpublic The resource can be accessed from any scope.

protected The resource can only be accessed from within the class where it is defined and its descendants.

private The resource can only be accessed from within the class where it is defined.

final The resource is accessible from any scope, but cannot be overridden in descendant classes.

Page 11: OOP in PHP

DECLARING AND ACCESSING PROPERTIESclass foo {

public $bar;

protected $baz;

private $bas;

public $var1 = "Test"; // String

public $var2 = 1.23; // Numeric value

public $var3 = array (1, 2, 3);

}

Page 12: OOP in PHP

CONSTANTS, STATIC METHODS AND PROPERTIESclass foo {

static $bar = "bat";

public static function baz()

{

echo "Hello World";

}

}

$foo = new foo();

$foo->baz();

echo $foo->bar;

Hello WorldPHP Strict Standards: Accessing static property foo::$bar as non static in PHPDocument1 on line 17Strict Standards: Accessing static property foo::$bar as non static in PHPDocument1 on line 1

Page 13: OOP in PHP

CLASS CONSTANTS

class foo {

const BAR = "Hello World";

}

echo foo::BAR;

Page 14: OOP in PHP

INTERFACES AND ABSTRACT CLASSES•An abstract class essentially defines the basic skeleton of a specific type of encapsulated entity.

•Interfaces, on the other hand, are used to specify an API that a class must implement.

Page 15: OOP in PHP

EXCEPTIONS• Exceptions provide an error control mechanism that is more fine-grained than traditional PHP fault handling, and that allows for a much greater degree of control.

• Key differences between “regular” PHP errors and exceptions:

• Exceptions are objects, created (or “thrown”) when an error occurs

• Exceptions can be handled at different points in a script’s execution, and different types of exceptions can be handled by separate portions of a script’s code

• All unhandled exceptions are fatal

• Exceptions can be thrown from the __construct method on failure

• Exceptions change the flow of the application

Page 16: OOP in PHP

OBJECT ORIENTED

PROGRAMMING IN PHP