Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects."— Presentation transcript:

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

2 Recap Introducing PHP Server Side Scripting Variables Flow Control Operators, Conditionals Form Handling

3 This Week Extending PHP Functions Objects

4 Why Functions? Less Typing Less Syntax Less Errors Quicker loading time Quicker Execution time Simplify Logic

5 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…

6 Defining Functions function function_name(parameter, parameter) { //Statements }

7 Returning Values You can return variables return $n1; You can return an array of values return array($n1, $n2, $n3);

8 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)); }

9 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”;

10 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”

11 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.

12 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?

13 Objects in PHP We can define classes in PHP class User { public $name, $password; function f1() { //Code Here } Notice, no semicolon!

14 Objects in PHP We can create instances of our class; $object = new User; $object->name = “Ken”; $object->password = “secret”; print_r($object); Notice, no $

15 Cloning an Object What would happen here? $object1 = new User; $object1->name = “Ken”; $object2 = $object1; $object2->name = “Jeff”; echo “object 1 name = “. $object1->name. “ ”; echo “object 2 name = “. $object2->name;

16 Cloning an Object An Alternative $object1 = new User; $object1->name = “Ken”; $object2 = clone $object1; $object2->name = “Jeff”; echo “object 1 name = “. $object1->name. “ ”; echo “object 2 name = “. $object2->name;

17 Constructors You could use the same name as the class (User), or, you could use __construct() class User { function __construct() { $username = “Guest”; } public $username; }

18 Destructors We can also have destructor methods; class User { function __destruct() { //Code }

19 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

20 Inheritance Without wanting to go into details, PHP also supports inheritance class Subscriber extends User { }

21 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.

22 Exercise - Example FoE Mtg Title 13/12/2011 Date ASEAN-QA meeting, review the handbook first. Detail Submit 2 1

23 Exercise - Example December Foe Mtg in 1 day(s)

24 Exercise Example FoE Mtg Title 13/1/2013 Date ASEAN-QA meeting, review the handbook first. Detail Appointment Detail 3


Download ppt "269200 Web Programming Language Week 6 Dr. Ken Cosh PHP Functions & Objects."

Similar presentations


Ads by Google