ICS 220 – Data Structures and Algorithms Lecture 11 Dr. Ken Cosh.
269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects.
-
Upload
vaughn-linder -
Category
Documents
-
view
223 -
download
2
Transcript of 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects.
![Page 1: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/1.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/2.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/3.jpg)
This Week
• Extending PHP
• Functions
• Objects
![Page 4: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/4.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/5.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/6.jpg)
Defining Functions
function function_name(parameter, parameter)
{
//Statements
}
![Page 7: 269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/7.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/8.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/9.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/10.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/11.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/12.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/13.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/14.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/15.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/16.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/17.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/18.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/19.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/20.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/21.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/22.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/23.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032516/56649c755503460f949284a9/html5/thumbnails/24.jpg)
Exercise Example
FoE MtgTitle
13/1/2013Date
ASEAN-QA meeting, review the handbook first.
Detail
Appointment Detail
3