Web Technology Solutions
Post on 21-Jan-2016
15 Views
Preview:
DESCRIPTION
Transcript
Date : 1/12/2012
Web Technology SolutionsClass: Three-Tier Application Architecture and more PHP Programming Practice
Tonight
✤ Three Tier Architecture PHP and its
oppositionPHP Syntax Review and Simple
Debugging Techniques.Lab
Lab Preview
✤ PHP Basics: Working with variables, conditionals, managing arrays, clean commenting, and debugging.
✤ Homework: documentation and design.
System Recap
✤ software system is a collection of interacting software components. Recall that the components of a system can also be considered as systems, called subsystems. Those subsystems (components) of a system that directly interact with each other do so through interfaces
Modern web apps excel at all three - twitter, Facebook, digg, flickr, etc
Server Side SystemsAllow for High Availability(HA), Dynamic Data Interaction, Data Storage
Web Applications Systems
Which Systems To Use?
Selecting Systems
✤ Being a technologist is understanding which system offers their client the maximum of benefits. How do you choose?
✤ Modern Web Applications collect various subsystems and make them their own system.
✤ Solves for HA, Interaction, Storage and Retrieval.
Application Architecture
System Models
✤ An Architecture Defines: What Important Systems are Needed and their Interfaces.
✤ A model is a simplified way to represent or think about a system of interest. Models are often represented using diagrams to show how components are related and organized.
✤ Two System Examples: Web App, Databases.
Example System
Common Models
✤ Database
Common Models
✤ Web Servers
Common Models
✤ Internet
Common Models
✤ Load Balancer
Web Systems
✤ Web Applications should scale gracefully. LAMP allows for that
✤ Note: the load balancer
✤ Multiple Redundant Web Servers
✤ Multiple DBs (master and slave)
✤ What is missing? CDN’s
MySQL ExampleMySQL comes with many sub-systems
How to Document a Model
✤ Models and Architecture add value as design documents.
✤ Include only the most important components of a system.
✤ Hide Details
How to Document a Model
✤ Hide the details
✤ Reduce complexity and increases conceptualization making for easy buy-off.
✤ Avoid information overload
✤ Stakeholders and non-techies don’t care.
✤ Example: Three Tier system
3-Tier Web System
✤ Typical Web solution where each tier provides specific types of service.
✤ 1. Presentation Layer: produces the user interface
✤ 2. Business Logic Layer: implements applications functionality
✤ 3. Data services Layer: provides data storage for the application
✤ Example: Detailed Model (note subsystems)
Why PHP?
PHP
✤ PHP: Ubiquitous. Easy to host and learn.
✤ PHP: tied in harmony to Linux, Apache, MySQL. Loads of devs and huge community.
✤ PHP is criticized because it is not strict in its promotion of best-of-breed coding practices. See php.net comments.
✤ PHP and PECL extend PHP
✤ PHP is liberating to rapidly develop ‘simple sites’ but scalable web apps?
PHP vs. Other
✤ The debate with PHP vs. others. Understand each has their benefit.
✤ Ruby
✤ C#
✤ Java
✤ Python
PHP vs. Ruby
✤ PHP vs. Ruby (Rails)
✤ Syntax akin to smalltalk. PHP is C based.
✤ PHP easier to host (rails ssh, root)
✤ PHP not strict. Ruby all OOP (everything object)
✤ PHP has many frameworks,
✤ Rails is MVC
PHP vs. C#
✤ PHP vs. C# (.net)
✤ C# tied to the .NET framework
✤ C# bindings to allow for desktop apps
✤ Akin to Java in ability and behavior.
✤ has an MVC framework.
PHP vs. Python
✤ PHP vs. Python (Django)
✤ Python has strict syntax control structure
✤ Python benchmarks
✤ PHP primarily web based - python extends beyond the web.
✤ Bad programmers are able to write bad programs in any language
Best Practice
✤ Bad PHP: Three layers all in one (spaghetti code)
✤ Good PHP (MVC Design Pattern)
✤ Presentation: Templates (minimal PHP)
✤ Logic: All PHP to handle data transaction and state
✤ Business: PHP is used to abstract the Database. No real native SQL calls. PHP can be used for good or bad.
Best Practice Tip
✤ Always develop a high level system architecture as part of the documentation deliverable.
✤ Clients love to know you’re taking care of the details.
Best Practice Tip
✤ Prepare detailed low-level system and system diagrams for areas of risk or what I would be personally responsible for and will require prototyping.
✤ risk: something I haven’t coded before
Best Practice Tip
✤ Focus on Rapid Application Development Techniques
✤ Be Agile
✤ Update quickly
✤ Verify assumptions
✤ Ask questions, find improvements
Best Practice Tip
✤ Keep Documentation Current. Things change!
Final Project Documentation
Start now.Lesson 3: Start documentingLesson 4: Much prototyping.Lesson 5: Documentation Complete.You’re building a professional application.
Tools
FTP
✤ Mac
✤ Transmit
✤ Cyberduck
✤ Coda
✤ PC
✤ SmartFTP
✤ FileZilla
✤ Firefox
IDE
✤ Mac
✤ Coda
✤ Eclipse
✤ Textmate
✤ Dreamweaver
✤ PC
✤ PHP Designer
✤ Zend Developer
✤ notepad++ \ dreamweaver
Firebuga preview
Programming PHP
PHP on Linux: First Steps
✤ Understanding how PHP is setup and configured on your installation of linux is critical to your success.
✤ phpinfo()
✤ php.ini
✤ Denied function, extensions, modules.
PHP Basics
✤ All php code must start with <?
✤ All php code must end with ?>
✤ You might see <?php which is fine as well.
PHP Basics
✤ Every PHP statement must end in a semicolon?
✤ echo “i love php”;
PHP Basics
✤ if a webpage contains PHP, keep the extension as .php
✤ do not name it html.
PHP Variables
✤ Variables store basic units of data that can control an application
✤ Variables in PHP start with a $ sign. Must start with char or _
✤ for example: $player
✤ setting variable: $player = “poodle”;
✤ variable reference: $player =& $music (points to same content in memory)
✤ restricted: $this (used in classes)
✤ destroy: unset();
PHP Restricted Keywords
✤ Abstract, And, array(), As, Break, Case, catch, cfunction, Class, clone, Const, Continue, Declare, Default, die(), Do, echo(), Else, elseif, empty, enddeclare, endfor, endforeach, endif, endswitch, endwhile,final, for, foreach, function, global, if
HTTP GET
✤ This method appends the form-data to the URL in name/value pairs
✤ This method is useful for form submissions where a user want to bookmark the result
✤ There is a limit to how much data you can place in a URL (varies between browsers), therefore, you cannot be sure that all of the form-data will be correctly transferred
✤ Never use the "get" method to pass sensitive information! (password or other sensitive information will be visible in the browser's address bar)
HTTP POST
✤ This method sends the form-data as an HTTP post transaction
✤ Form submissions with the "post" method cannot be bookmarked
✤ The "post" method is more robust and secure than "get", and "post" does not have size limitations
PHP Global Variables
✤ Pre-Defined SuperGlobal Vars - available in all scope.✤ usually indicated by underscore and caps✤ $GLOBALS✤ $_SERVER✤ $_GET✤ $_POST✤ $_COOKIE✤ $_SESSION✤ $_FILES✤ $_ENV✤ old style from php3 $HTTP_COOKIE_VARS
PHP Variables
✤ Typically a variable has a lifespan of the current page (including pages that are inserted within the page you’re working).
✤ In OOP there are extended definitions of a variable that restrict usage.
✤ Golden Rule: Avoid the urge to scope globally.
PHP Commenting
✤ Super basic:
// this is a single line comment
/* this is a comment */
/*
* this is a comment
*/
✤ best practice: good commenting
PHP Commenting
/*
* Dev: Lincoln Mongillo
* Time Updated: 09.12.08 2:30pm
* File: email-validation.php
* Desc: validates a users email address
*/
PHP Data Types
✤ Booleans
✤ Integers
✤ Floats
✤ Strings
✤ Arrays
✤ Objects
✤ Resource
Boolean
✤ very basic type that allows you define what is True or False.
<? $playerActive = True; ?>
✤ True or False is often times what is returned to you when conditionally checking.
✤ PHP is often criticized because boolean expectations are not correct in rare-cases.
Boolean
✤ False in PHP are:
✤ keyword “false”
✤ int 0
✤ floating point 0.0
✤ empty string
✤ array with zero elements
✤ obj with no vals
✤ NULL
Integers
✤ a simple number. Note: an integer does not include a decimal.
<?php
$a = 3232; // decimal number
$a = -543; // a negative number
$a = 0133; // octal number (equivalent to 83 decimal)
is_int()
?>
Floats
✤ a simple decimal representation of an integer. Note: different versions of PHP are more precise. hint: not precise - warning!
<?php
$a = 1.234;
$b = 1.2e3; //exponential
is_float()
?>
Strings
✤ a series of character data. PHP can support 256 characters. PHP 6 aims to support UTF-8 and unicode, allowing for more character types.
<?php
echo 'I own an apple iPod';
echo “I can also \n put in <b>html</b>”;
?>
Arrays
✤ “An ordered map”, meaning a value is tied to a key.
<?php
$arr = array("name" => "bob", 12 => true);echo $arr["name"]; // bob
?>
✤ keys: associative, numerical or both!
✤ values: can be any type, including additional arrays
Constants
✤ Allows you to define a non-changing data value.
<?php
define("URL", "http:www.site.com");
define("PASSWORD", "bosco");
?>
✤ By convention: usually uppercase.
Expression
✤ Expressions are the basic building blocks of PHP (or most any programming language)
$var = 5; // setting
$var == 5; // equal to (=== to strict)
$var != 5 // not equal;
$var++; // increment; (or --)
Expression
✤ Expressions are the basic building blocks of PHP (or most any programming language)
< //less than
> //greater than
<= //less then or equal to
>= //greater than or equal to
Operators
✤ Operators allow you to extend on an expression. There are three types
✤ unary: operates on one expression
✤ binary: operates against two expressions
✤ ternary: operates on two, depending on a third option (like an if-statement)
Unary Operators
✤ unary: operates on one expression
✤ The most common example is the !, which means “is not”
if(!($dog)){
echo “than cat”:
}
Binary Operators
✤ binary: operates on two expression
✤ The most common example is the || or &&.
✤ || (double pipe) is “or”
✤ && (double amps) is “and”
if(($dog == “chuck”) && ($cat == “jim”)){
echo “than the dogs name is chuck and the cats jim”;
}
Ternary Operators
✤ ternary: operates on two, depending on a third option (like an if-statement)
✤ The basics work like an if statement (?:)
✤ The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
Control Operators
✤ ifelseelseifwhiledo\whilefor
✤ Take control of your logic with basic control structures. PHP uses many classics from more established languages, like C
✤ foreachbreakcontinue
✤ return
✤ require
✤ include
if statement
$dog = true;
if($dog){
$myPet = “is a dog”;
}
//eval to true
if-else statement
$dog = true;
if($dog){
$myPet = “is a dog”;
}else{
$myPet = “is not a dog”;
}
//eval to true
if-else statement
$dog = true;
if($dog){
$myPet = “is a dog”;
}elseif($dog = “dead”){
$myPet = “is not a living dog”;
}else{
}
switch statement
switch ($i) {
case 0:
echo "i equals 0";
case 1:
echo "i equals 1";
case 2:
echo "i equals 2";
break;
default:
echo “no number”;
}
While Loop
$i = 1;
while ($i <= 100) {
echo “currently “ . $i . “<br/>”;
echo “add a few “ . $i++;
}
//while something is true, do something
Do While Loop
<?php
//Do something. Check if true. Continue
$i = 0;
do {
echo $i++;
} while ($i > 0);
?>
5min Developer ChallengeCreate a while loop that counts up to a hundred on the screen with line breaks
For Loop
<?php
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
?>
For Each Loop
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
?>
5min Developer ChallengeCreate a foreach loop that prints the contents of an array of presidents.
Functions
✤ functions are incapsulated and allows for code to be portable and reusable (if developed correctly)
function sayMyName($first, $last)
{
echo "Example function.\n";
var retval = “my name is “. $first . “ “ .$last;
return $retval;
}
PHP Functions List
✤ See PHP website for a massive amount of predefined functions available to you via the PHP API (application programming interface).
Final Project
✤ Your final project is an online survey. This application will allow admin to create various surveys, collect user response, and analyze data.
✤ I am your client. I want a survey tool that I can gather information from people. I want it to be easy and secure. Ask me questions.
Final Project
✤ Data Model will be shown next week
✤ Surveys
✤ Questions and Answers
✤ Responses and Response Answers
✤ Users (including admin)
✤ Possible methods
✤ create new question, delete question, modify question
✤ show survey results, etc
Lab & Next Week
✤ Documentation:
✤ Develop Application Architecture (visio is great for this)
✤ PHP:
✤ Create a simple survey and PHP script that allows for flow control using conditionals.
✤ Create a simple form and PHP script that validates user input.
See you Thursday!
Lab & Next Week
✤ “PHP Abstraction, Server Side Includes, Self Pointing PHP Scripts, and Function Development.”
✤ Reading for Next Week
✤ Chapter 3
See you Thursday!
top related