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

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

PHP I.
Error HandlingPHPMay-2007 : [#] PHP Error Handling.
CS 22: Enhanced Web Site Design - Week 8Slide 1 of 15 Enhanced Web Site Design Stanford University Continuing Studies CS 22 Mark Branom
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
PHP and MySQL Database. Connecting to MySQL Note: you need to make sure that you have MySQL software properly installed on your computer before you attempt.
The Web Warrior Guide to Web Design Technologies
ITC 240: Web Application Programming
Object-Oriented PHP (1)
CST JavaScript Validating Form Data with JavaScript.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
PHP : Hypertext Preprocessor
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
INTERNET APPLICATION DEVELOPMENT For More visit:
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
BEST PRACTICES - Java By Configuration Use global-forwards/results Helps to avoid duplicate jsp files and redundancy forward mapping.
PHP Tutorial - Anas Jaghoub Chapter 2 Control Structures.
PHP Flow Control Loops, Functions, Return, Exit, Require, Try...Catch Software University SoftUni Team Technical Trainers.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
NMD202 Web Scripting Week3. What we will cover today Includes Exercises PHP Forms Exercises Server side validation Exercises.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Web Programming Language Week 5 Dr. Ken Cosh Introducing PHP 1.
Dynamic web content HTTP and HTML: Berners-Lee’s Basics.
PHP Constructs Advance Database Management Systems Lab no.3.
Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
1Introduction to PHP 5 Presented by Brett Buddin.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Display Page (HTML/CSS)
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
PHP Form Processing * referenced from
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
15 – PHP(5) Informatics Department Parahyangan Catholic University.
Batch Files Flow of Control to Strengthen Copyright © by Curt Hill.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
PHP using MySQL Database for Web Development (part II)
Introduction to Dynamic Web Programming
PHP Classes and Objects
PDO Database Connections
Web Systems Development (CSC-215)
More Selections BIS1523 – Lecture 9.
Phil Tayco Slide version 1.0 Created Oct 2, 2017
PDO Database Connections
PDO Database Connections
ISC440: Web Programming 2 AJAX
PHP.
CSCI 297 Scripting Languages Day Seven
Web Programming Language
Object-Oriented Programming in PHP
PHP Forms and Databases.
Object-Oriented PHP (1)
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

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; $object->name = “Ken”; $object->password = “secret”; print_r($object); Notice, no $

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;

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;

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 FoE Mtg Title 13/12/2011 Date ASEAN-QA meeting, review the handbook first. Detail Submit 2 1

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

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