2010/11 : [1]building web applications using mysql and php (w1)oo php php classes and object...

37
2010/11 : [1] Building Web Applications using MySQL and PHP (W1) OO PHP PHP Classes and Object Orientation

Upload: doris-powers

Post on 14-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP

PHP Classesand

Object Orientation

Page 2: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [2]Building Web Applications using MySQL and PHP (W1)OO PHP

Reminder... a function

• Reusable piece of code

• Has its own ‘local scope’.

function my_func($arg1, arg2)

{

//function statements

}

Page 3: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [3]Building Web Applications using MySQL and PHP (W1)OO PHP

...give the function something (arguments), it does something with them, and then returns a result...

Action or Method

Conceptually, what does a function represent?

Page 4: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [4]Building Web Applications using MySQL and PHP (W1)OO PHP

What is a class?

Conceptually, a class represents an object, with associated

methods and variables

Page 5: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [5]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

class Dog

{

public $name;

function bark()

{

echo 'Woof';

}

}?>

Class Definition

An example class definition for a dog. Every dog object has a single attribute, the name, and can perform the action of barking.

Page 6: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [6]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

class dog

{

public $name;

function bark()

{

echo 'Woof';

}

}?>

Class Definition

Define the name of the class.

Page 7: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [7]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

class dog

{

public $name;

function bark()

{

echo 'Woof';

}

}?>

Class Definition

Define an object attribute (variable) the dog’s name.

Page 8: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [8]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

class dog

{

public $name;

function bark()

{

echo 'Woof';

}

}?>

Class Definition

Define an object action (function), the dog’s bark.

Page 9: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [9]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

class dog

{

public $name;

function bark()

{

echo 'Woof';

}

}?>

Class Definition

End the class definition

Page 10: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [10]Building Web Applications using MySQL and PHP (W1)OO PHP

Similar to defining a function...

The definition does not do anything by itself. It is a blueprint, or description, of an object. To do something, you need to use

the class...

Class definition

Page 11: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [11]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

require('dog.class.php');

$puppy = new dog();

$puppy->name='Rover';

echo $puppy->name.' says';

$puppy->bark();

?>

Class usage

Page 12: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [12]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

require('dog.class.php');

$puppy = new dog();

$puppy->name='Rover';

echo $puppy->name.' says';

$puppy->bark();

?>

Class usage

Include the class definition

Page 13: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [13]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

require('dog.class.php');

$puppy = new dog();

$puppy->name='Rover';

echo $puppy->name.' says';

$puppy->bark();

?>

Class usage

Create a new instance (object) of the class

Page 14: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [14]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

require('dog.class.php');

$puppy = new dog();

$puppy->name='Rover';

echo $puppy->name.' says';

$puppy->bark();

?>

Class usage

Set the name variable of this instance to ‘Rover'.

Page 15: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [15]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

require('dog.class.php');

$puppy = new dog();

$puppy->name='Rover';

echo $puppy->name.' says';

$puppy->bark();

?>

Class usage

Use the name attribute of this instance in an echo statement.

Page 16: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [16]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

require('dog.class.php');

$puppy = new dog();

$puppy->name='Rover';

echo $puppy->name.' says';

$puppy->bark();

?>

Class usage

Use the object method bark().

Page 17: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [17]Building Web Applications using MySQL and PHP (W1)OO PHP

$puppy->name = 'Rover';

The most common mistake is to use more than one dollar sign when accessing variables. The following means something entirely different.

$puppy->$name = 'Rover';

One dollar and one only...

Page 18: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [18]Building Web Applications using MySQL and PHP (W1)OO PHP

• If you need to use the class variables within any class methods, use the special variable $this in the definition:

class Dog

{

public $name;

function bark()

{

echo $this->name.' says Woof!';

}

}

Using attributes within the class

Page 19: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [19]Building Web Applications using MySQL and PHP (W1)OO PHP

• A constructor method is a function that is automatically executed when the class is first instantiated.

• Create a constructor by including a function within the class definition with the name __construct

• Remember, if the constructor requires arguments, they must be passed when it is instantiated!

Constructor methods

Page 20: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [20]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

class Dog

{

var $name;

function __construct($nametext)

{

$this->name=$nametext;

}

function bark(){echo 'Woof!';}

}?>

Constructor example

Page 21: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [21]Building Web Applications using MySQL and PHP (W1)OO PHP

<?php

//...

$puppy = new Dog('Rover');

//...

?>

[example file: classes2.php]

Constructor example

Constructor arguments are passed during the instantiation of the object.

Page 22: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [22]Building Web Applications using MySQL and PHP (W1)OO PHP

• Like functions, each instantiated object has its own local scope.

e.g. If 2 different dog objects are instantiated, $puppy1 and $puppy2, the two dog names $puppy1->name and $puppy2->name are entirely independent.

Class scope

$puppy1:Dog

name

Jack

$puppy2:Dog

name

Lassie

Page 23: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [23]Building Web Applications using MySQL and PHP (W1)OO PHP

HOE:

Using Classes

Page 24: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [24]Building Web Applications using MySQL and PHP (W1)OO PHP

• The real power of using classes is the property of inheritance – creating a hierarchy of interlinked classes.

Inheritance

Dog

Poodle Alsatian

parent

children

Page 25: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [25]Building Web Applications using MySQL and PHP (W1)OO PHP

• The child classes ‘inherit’ all the methods and variables of the parent class, and can add extra ones of their own.

e.g. The child class poodle inherits the variable ‘name’ and method ‘bark’ from the Dog class, and can add extra ones...

Inheritance

Page 26: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [26]Building Web Applications using MySQL and PHP (W1)OO PHP

The American Kennel Club (AKC) recognises three sizes of poodle – Standard, Miniature and Toy...

class Poodle extends Dog {

public $type;

function set_type($height) {

if ($height<10) {

$this->type = 'Toy';

} elseif ($height>15) {

$this->type = 'Standard';

} else {

$this->type = 'Miniature';

}

}

}

Inheritance example

Note the use of the extends keyword to indicate that the Poodle class is a child of the Dog class...

Page 27: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [27]Building Web Applications using MySQL and PHP (W1)OO PHP

...

$puppy = new Poodle('Oscar');

$puppy-set_type(12); //12 inches high!

echo 'Poodle is called '.$puppy->name;

echo ", of type {$puppy->type}, saying ";

echo $puppy->bark();

...

[example file: classes3.php]

Inheritance example

Page 28: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [28]Building Web Applications using MySQL and PHP (W1)OO PHP

• It is possible to over-ride a parent method with a new method if it is given the same name in the child class

class Poodle extends Dog

{ // ...

function bark(){

echo 'Yip!';

} //...

} [example file: classes4.php]

...a poodle will always 'Yip!'

Page 29: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [29]Building Web Applications using MySQL and PHP (W1)OO PHP

• If the child class possesses a constructor function, it is executed and any parent constructor is ignored.

• If the child class does not have a constructor, the parent's constructor is executed.

• If the child and parent do not have a constructor, the grandparent constructor is attempted…etc…

Child constructors?

Page 30: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [30]Building Web Applications using MySQL and PHP (W1)OO PHP

• It is perfectly possible to include objects within another object...

class Dogtag {

public $words;

}

class Dog {

public $name;

public $tag;

function bark() { echo "Woof!\n";}

}

Objects within Objects

...$puppy = new Dog();$puppy->name = "Rover";$puppy->tag = new Dogtag();$puppy->tag->words = "blah";...

Page 31: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [31]Building Web Applications using MySQL and PHP (W1)OO PHP

• So far our objects have not been destroyed till the end of our scripts...

• Like variables, it is possible to explicitly destroy an object using the unset() function.

Deleting objects

Page 32: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [32]Building Web Applications using MySQL and PHP (W1)OO PHP

• Entire objects can be passed as arguments to functions, and can use all methods/variables within the function.

• However, unlike normal function arguments the object is not copied when passed as an argument.

A copy, or not a copy

Page 33: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [33]Building Web Applications using MySQL and PHP (W1)OO PHP

HOE:

Class Inheritance

Page 34: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [34]Building Web Applications using MySQL and PHP (W1)OO PHP

Reason 1

Once you have your head round the concept of objects, intuitively named object orientated code becomes easy to understand.

e.g.

$order->display_basket();

$user->card[2]->pay($order);

$order->display_status();

Why Object Orientate?

Page 35: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [35]Building Web Applications using MySQL and PHP (W1)OO PHP

Reason 2

Existing code becomes easier to maintain.

e.g. If you want to extend the capability of a piece of code, you can merely edit the class definitions...

Why Object Orientate?

Page 36: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [36]Building Web Applications using MySQL and PHP (W1)OO PHP

Reason 3

New code becomes much quicker to write once you have a suitable class library.

e.g. Need a new object? Usually can extend an existing object. A lot of high quality code is distributed as classes (e.g. http://pear.php.net).

Why Object Orientate?

Page 37: 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation

2010/11 : [37]Building Web Applications using MySQL and PHP (W1)OO PHP

• We have really only touched the edge of object orientated programming...

http://www.php.net/manual/en/language.oop.php

• ...but I don't want to confuse you too much!

There is a lot more