Except where otherwise noted, the contents of this document are Copyright 2012–2013 Morgan Doocy. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
// construct an object $name = new ClassName(parameters); // access an object's field (if the field is public) $name->fieldName // call an object's method $name->methodName(parameters);
$zip = new ZipArchive(); $zip->open("moviefiles.zip"); $zip->extractTo("images/"); $zip->close();
class_exists
// create an HTTP request to fetch student.php $req = new HttpRequest("student.php", HttpRequest::METH_GET); $params = array("first_name" => $fname, "last_name" => $lname); $req->addPostFields($params); // send request and examine result $req->send(); $http_result_code = $req->getResponseCode(); // 200 means OK print "$http_result_code\n"; print $req->getResponseBody();
HttpRequest
object can fetch a document from the webclass ClassName { // fields - data inside each object public $name; // public field private $name; // private field // constructor - initializes each object's state public function __construct(parameters) { statement(s); } // method - behavior of each object public function name(parameters) { statements; } }
$this
<?php class Point { public $x; public $y; // equivalent of a Java constructor public function __construct($x, $y) { $this->x = $x; $this->y = $y; } public function distance($p) { $dx = $this->x - $p->x; $dy = $this->y - $p->y; return sqrt($dx * $dx + $dy * $dy); } // equivalent of Java's toString method public function __toString() { return "(" . $this->x . ", " . $this->y . ")"; } } ?>
<?php // this code could go into a file named use_point.php include("Point.php"); $p1 = new Point(0, 0); $p2 = new Point(4, 3); print "Distance between $p1 and $p2 is " . $p1->distance($p2) . "\n\n"; var_dump($p2); // var_dump prints detailed state of an object ?>
Distance between (0, 0) and (4, 3) is 5 object(Point)[2] public 'x' => int 4 public 'y' => int 3
$p1
and $p2
are references to Point
objects
class ClassName extends ClassName { ... }
class Point3D extends Point { public $z; public function __construct($x, $y, $z) { parent::__construct($x, $y); $this->z = $z; } ... }
static $name = value; // declaring a static field const $name = value; // declaring a static constant
// declaring a static method
public static function name(parameters) {
statements;
}
ClassName::methodName(parameters); // calling a static method (outside class) self::methodName(parameters); // calling a static method (within class)
interface InterfaceName { public function name(parameters); public function name(parameters); ... } class ClassName implements InterfaceName { ...
abstract class ClassName { abstract public function name(parameters); ... }
Data encapsulation
Pluggable/interchangeable user interfaces.
Simple, concise control flow logic.
<?php
class Employee {
private $db;
private $id;
public $first_name;
public $last_name;
public $ssn;
// equivalent of a Java constructor
public function __construct($first_name, $last_name, $ssn) {
$this->db = new PDO("mysql:dbname=hr;host=localhost;charset=utf8", "hr", "r3s0urc3s");
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->ssn = $ssn;
}
public function save() {
$stmt = $this->db->prepare("INSERT INTO employees (first_name, last_name, ssn)
VALUES (:fn, :ln, :ssn)");
$stmt->execute(array(':fn' => $this->first_name, ':ln' => $this->last_name, ':ssn' => $this->ssn));
$this->id = $stmt->lastInsertId();
}
public function load($id) {
$stmt = $this->db->query("SELECT FROM employees WHERE id = $id");
$employee = $stmt->fetch(PDO::FETCH_ASSOC);
foreach ($employee as $col => $val) {
$this->$col = $val;
}
}
}
?>
Allows each to be used independently and interchangeably
Objects or data structures that store the information.
Examples:
A user interface with pieces of data plugged in, and event handlers to respond to user input.
Loads & processes data, and decides what UI should be presented to the user.
input-related tasks:
output-related tasks:
between Model and Controller:
sometimes the Model is a self-sufficient object, with functions to load, add, remove, etc.
sometimes the Controller takes care of loading, modifying, and saving data, in lieu of a self-sufficient Model object
between Controller and View:
Widely used in Web Application Frameworks (e.g., ASP.NET MVC, Rails, JSP)
Often the natural result of factoring out redundancy
Controller–Model separation is sometimes not necessitated
Allows “front-end” designer and “back-end” developer to be responsible for separate files
Can be considered “templates” into which pieces of data are injected
<?= ... ?>
Drawback: Some frequently-performed operations are not concise
Can use plug-in templating systems to be more concise and save work
“Super-structure” built on top of a language to enable rapid application development
Examples: