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 This Week • Extending PHP • • Functions Objects Why Functions? • Less Typing • Less Syntax • Less Errors • Quicker loading time • Quicker Execution time • Simplify Logic 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… Defining Functions function function_name(parameter, parameter) { //Statements } Returning Values • You can return variables • return $n1; • You can return an array of values • return array($n1, $n2, $n3); 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)); } 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 page include “functions.php”; 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” 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. function_exists • PHP is continually developed, there are multiple versions. • We may need to check if a function exists in our version if(function_exists(“array_combine”)) { …} • array_combine() is specific to PHP5 • Think! How might needing to do this change between php and javascript? Objects in PHP • We can define classes in PHP class User { public $name, $password; function f1() { //Code Here } } Notice, no semicolon! Objects in PHP • We can create instances of our class; $object = new User; Notice, no $ $object->name = “Ken”; $object->password = “secret”; print_r($object); 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; 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; Constructors • You could use the same name as the class (User), or, you could use __construct() class User { function __construct() { $username = “Guest”; } public $username; } Destructors • We can also have destructor methods; class User { function __destruct() { //Code } } 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 Inheritance • Without wanting to go into details, PHP also supports inheritance class Subscriber extends User { } 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. Exercise - Example 1 Title Date Detail FoE Mtg 13/12/2011 ASEAN-QA meeting, review the handbook first. Submit 2 Exercise - Example 2 December 2011 Foe Mtg in 1 day(s) 3 Exercise Example 3 Appointment Detail Title Date Detail FoE Mtg 13/1/2013 ASEAN-QA meeting, review the handbook first.
© Copyright 2024 Paperzz