Functions & Objects IDIA 618 Spring 2012 Bridget M. Blodgett.

Slides:



Advertisements
Similar presentations
Objectives Using functions to organize PHP code
Advertisements

JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
IS 1181 IS 118 Introduction to Development Tools Chapter 5 Reusing Code.
Guide To UNIX Using Linux Third Edition
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
PHP Workshop ‹#› PHP Classes and Object Orientation.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Practical PHP IDIA Spring 2012 Bridget M. Blodgett.
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
An Object-Oriented Approach to Programming Logic and Design
PHP Tutorial - Anas Jaghoub Chapter 2 Control Structures.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
PHP TUTORIAL. HISTORY OF PHP  PHP as it's known today is actually the successor to a product named PHP/FI.  Created in 1994 by Rasmus Lerdorf, the very.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
4.1 Instance Variables, Constructors, and Methods.
JavaScript, Fourth Edition
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
ClassesPHPMay-2007 : [‹#›] PHP Classes and Object Orientation.
Functions, Objects, and Programming IDIA 619 Spring 2014 Bridget M. Blodgett.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Chapter 2 User_defined Function. Chapter Goals In this chapter, you’ll learn all about PHP functions, including how : to create and invoke them, pass.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
CPS120: Introduction to Computer Science Lecture 14 Functions.
PHP Constructs Advance Database Management Systems Lab no.3.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PHP. PHP User Defined Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Controlling Program Flow with Decision Structures.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Chapter 11 Developing Object-Oriented PHP PHP Programming with MySQL Revised by A. Philipp – Spring 2010 (Rev SP’11)
Functions Inputs Output
More Selections BIS1523 – Lecture 9.
Coding Concepts (Basics)
An Introduction to Object Orientated Programming
Objectives In this chapter, you will:
Tonga Institute of Higher Education
1-6 Midterm Review.
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
ITM 352 Functions.
Presentation transcript:

Functions & Objects IDIA 618 Spring 2012 Bridget M. Blodgett

If and If…Else

Elseif

Switch Statements Very useful when an input can have multiple but pre-known values (like from a drop-down menu) <?php switch ($page) { //or : instead of { case "Home": echo "You selected Home"; break; case "About": echo "You selected About"; break; case "News": echo "You selected News"; break; case "Login": echo "You selected Login"; break; case "Links": echo "You selected Links"; break; } //or endswitch if used with : ?>

Looping While Do while For Break & Continue – Like in switch statements break will exit the loop structure altogether – Continue ends the current loop and starts the next one

Looping Exercises For this exercise you will be creating a loop that prints out a series of asterisk in a specific pattern Pattern 1: * Pattern 2: ***** Pattern 4: * ** *** ** * Pattern 3: * ** *** **** *****

Functions in PHP Functions allow you to create discrete sections of code that perform one action – This prevents the unnecessary duplication of code and saves time, money, and reduces errors and code complexity An object contains one or more related functions as well as all the data containers (variables) and manipulators (expressions) related to them within a class

Built-in Functions We’ve already seen some of PHP’s built-in functions – Print(); – Time(); The parentheses after the name tell the parser that this is a function and not some other construct.

Functions Functions can be (but are not required to be) passed some starting information, an argument When creating your own functions you must start the line with the declaration – function function_name() – Unlike variables function names are not case sensitive The curly brackets after the parentheses contain the code that will execute when the function is called

Function Outputs In addition to taking inputs called arguments functions can optionally return some type of output The output of functions can be nested inside other functions – Remember: PHP works from the inside out so the function on the inside will be completed before others on the outside – Example 5 -2

Exercise Add a function to the PHP script below which takes two parameters: (1) the number of years and (2) the amount of money to be put on a savings account. The function should calculate the amount that will have been reached after the given number of years. To print an amount with no more than two decimals, us the sprintf function, for instance, as follows: sprintf("%.2f", $amount ) ;

<?php /* Add function here */ print " The current interest rate is 5%. " ; print " When you deposit € 50 today, your savings accounts will have grown to €" ; print calculateAmount(10, 50) ; print " in 10 years. " ; print " When you deposit € 1,500 today, your savings will have grown to €" ; print calculateAmount(20, 1500) ; print " after 20 years. " ; ?>

Returning Multiple Pieces of Data Returning the results of an array is one way to return multiple pieces of data from within the same function – Example 5-3 uses an array to return each fixed name

Passing By Reference The & symbol, when put in front of a variable, tells the parser to pass a reference to the variable’s value, not the value itself – Dealing with values passed as an argument to a function is like Xeroxing the value of the variable, any changes you make to the copy won’t alter the original Passing by reference means handing off the original document for alteration

Global Variables It is possible to declare a variable that exists outside of a function ($a in the examples we’re using) a global variable from inside a function – Example 5-5 This means that nothing has to be passed directly to or from the function, it can access the variables directly – BUT! This makes the variables global permanently, a potential security issue

PHP and Object Oriented Programming (OOP) Objects take a series of related functions and group them together into one thing – Unlike libraries objects ALSO include all of the data/variables relevant to those functions as well Classes are the blueprints for the creation of an object, they contain all the needed information but the details are unique to each object – The object is referred to as an instance of that class

OOP When dealing with a program that is object oriented a new set of terms overrides the ones we’ve discussed so far: – Variables(data) become Properties – Functions become Methods While referring to their non-OOP names is not taboo, learning the correct terms is important for 1) understanding what others say, 2) distinguishing yourself as a savvy programmer/user

Encapsulation Much like you want to limit how much access is available to your variables, you will want to limit what can be accessed/manipulated about a class Encapsulation is writing a class in such a way that only its methods(functions) can be used to manipulate its properties(variables/data). – How can this be a good design principle? The methods you supply are known as the object’s interface.

Inheritance Occasionally we will want to make a class that has the same functionality as one we already have but that includes some refining or expansion upon the original – Instead of just updating the existing class, which can result in useless, bloated classes (and code) create a new class that inherits the structure of the original All of the properties and methods of the parent class will be available but you can add in unique features or methods that only apply to this sub- class

PHP and OOP PHP allows you to create and define classes for use within your program – Example 5-10 shows the syntax for doing this When dealing with a dynamic and user-driven website, how could the concept of classes be useful for handling all the different types of data? ((For right now… this is all you need to understand about PHP and OOP))

Include & Require Often some function will be needed in a program but you don’t have the time/skill/energy to create it yourself – Libraries are a method of creating suites of common, useful functions At the start of the PHP program simply write include “yourlibrary.php”; to add these functions to your program – include_once will include the library if it hasn’t done so yet and ignore the include if it has. Why is this useful? – require or require_once means that if the parser fails at including the library the code will fail altogether. Why is this useful?