269200 web programming language week 6 dr. ken cosh php functions & objects

24
269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Upload: vaughn-linder

Post on 14-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

269200 Web Programming

LanguageWeek 6

Dr. Ken Cosh

PHP Functions & Objects

Page 2: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Recap

• Introducing PHP

• Server Side Scripting

• Variables

• Flow Control

• Operators, Conditionals

• Form Handling

Page 3: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

This Week

• Extending PHP

• Functions

• Objects

Page 4: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Why Functions?

• Less Typing

• Less Syntax

• Less Errors

• Quicker loading time

• Quicker Execution time

• Simplify Logic

Page 5: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Existing Functions

• PHP has hundreds of functions…

• echo strrev(“.dlrow olleH”);

• echo str_repeat(“Hip “, 2);

• echo strtoupper(“hooray!”);

• echo ucfirst(“ken”);

• You will gradually pick up more functions as you face more challenges…

Page 6: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Defining Functions

function function_name(parameter, parameter)

{

//Statements

}

Page 7: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Returning Values

• You can return variables

• return $n1;

• You can return an array of values

• return array($n1, $n2, $n3);

Page 8: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Reference Parameters

• You can prefix a variable with & to make it a reference variable

• i.e. passes a reference to the value, rather than the variable itself.

• This allows your function to directly modify the variable’s value.

function fix_names(&$n1, &$n2)

{

$n1 = ucfirst(strtolower($n1));

$n2 = ucfirst(strtolower($n2));

}

Page 9: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Where to function?

• You could write functions anywhere within your PHP files

• Overtime you are likely to create a lot of functions, so you could create a file of functions

• functions.php

• You can then include that file in your pageinclude “functions.php”;

Page 10: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

include & require

• include will attempt to include another file

• if the file doesn’t exist it will produce a warning

• require is the same, except if the file can’t be found it will produce a fatal error

require “function.php”

Page 11: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

include_once, require_once

• You may end up with multiple files including multiple other files.

• If file A includes file B and file C, and file B include file C, you could run into naming problems…

• include_once and require_once will fix it.

Page 12: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

function_exists

• PHP is continually developed, there are multiple versions.

• We may need to check if a function exists in our versionif(function_exists(“array_combine”)) { …}

• array_combine() is specific to PHP5

• Think! How might needing to do this change between php and javascript?

Page 13: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Objects in PHP

• We can define classes in PHP

class User

{

public $name, $password;

function f1()

{

//Code Here

}

}

Notice, no semicolon!

Page 14: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Objects in PHP

• We can create instances of our class;

$object = new User;

$object->name = “Ken”;

$object->password = “secret”;

print_r($object);

Notice, no $

Page 15: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Cloning an Object

• What would happen here?

$object1 = new User;

$object1->name = “Ken”;

$object2 = $object1;

$object2->name = “Jeff”;

echo “object 1 name = “ . $object1->name . “<br>”;

echo “object 2 name = “ . $object2->name;

Page 16: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Cloning an Object

• An Alternative

$object1 = new User;

$object1->name = “Ken”;

$object2 = clone $object1;

$object2->name = “Jeff”;

echo “object 1 name = “ . $object1->name . “<br>”;

echo “object 2 name = “ . $object2->name;

Page 17: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Constructors

• You could use the same name as the class (User), or, you could use __construct()

class User

{

function __construct()

{

$username = “Guest”;

}

public $username;

}

Page 18: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Destructors

• We can also have destructor methods;

class User

{

function __destruct()

{

//Code

}

}

Page 19: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Public, Protected, Private

• Of course, properties and methods can be public, protected or private in PHP;

• public – accessible anywhere

• protected – accessible from within the class, or subclasses

• private – only accessible from within the class

Page 20: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Inheritance

• Without wanting to go into details, PHP also supports inheritance

class Subscriber extends User

{

}

Page 21: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Exercise – Calendar Part I

• Create a form for submitting appointments to a calendar.

• The form should allow users to input the following information:-

• Title

• Date

• Details

• Once the user submits the form, the ‘event’ should be displayed on a calendar on a new page.

Page 22: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Exercise - Example

FoE MtgTitle

13/12/2011Date

ASEAN-QA meeting, review the handbook first.

Detail

SubmitSubmit 2

1

Page 23: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Exercise - Example

December 2011

3

2

Foe Mtg in 1 day(s)

Page 24: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects

Exercise Example

FoE MtgTitle

13/1/2013Date

ASEAN-QA meeting, review the handbook first.

Detail

Appointment Detail

3