Presentation is loading. Please wait.

Presentation is loading. Please wait.

Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1.

Similar presentations


Presentation on theme: "Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1."— Presentation transcript:

1 Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

2  Low Level C-like API ◦ fopen(), fread(), fwrite(), fclose(), … ◦ Data are treated as strings  Note that per-byte processing is extremely slow  File-wide Operations ◦ fpassthru(), readfile() – whole file is spilled to the output ◦ file_get_contents(), file_put_contents()  Read/write the entire file to/from a string ◦ file() – reads entire file as an array of lines 19. 3. 2015 by Martin Kruliš (v1.0)2

3  POSIX Based API ◦ Only partial functionality on non-POSIX OSes ◦ touch() – sets current time to a file ◦ tmpfile() – creates temporary file ◦ rename(), unlink() – unlink ~ delete ◦ mkdir(), rmdir() – make/remove dir  Synchronization Issues ◦ POSIX atomic operations (append, mkdir, …) ◦ Advisory file locking mechanism – flock()  Locks are bounded to processes, multi-threaded web server need not guarantee sync. between PHP scripts 19. 3. 2015 by Martin Kruliš (v1.0)3

4  File Properties (Size, Type, Creation Time, …) ◦ Dedicated functions for some properties  filesize(), filetype(), filectime() ◦ Reading all properties at once  stat(), lstat(), fstat()  POSIX Access Rights (rwx) ◦ Testing functions  is_file(), is_readable(), is_writeable(), … ◦ Modification of rights and owner  chmod(), chown(), chgrp() 19. 3. 2015 by Martin Kruliš (v1.0)4

5  Traversing Directories ◦ Iterative – opendir(), readdir(), closedir() ◦ scandir() – returns array with directory contents ◦ dir() – returns object of Directory class  Directory Paths ◦ glob() – expanding wildcards ◦ dirname(), basename() – get part of path ◦ pathinfo() – parses path and return components ◦ realpath() – converts symbolic to real path  Absolute path without links,./, and../ 19. 3. 2015 by Martin Kruliš (v1.0)5

6  CSV Files ◦ Comma (semicolon) separated values ◦ API uses file handlers from fopen()  fgetcsv() – reads one CSV line  fputcsv() – writes one CSV line  INI Files ◦ Configuration files with “name = value” items ◦ parse_ini_file() – reads the entire file and yields an associative array  Optionally divides the data to sections 19. 3. 2015 by Martin Kruliš (v1.0)6

7  ZIP Files ( *.zip ) ◦ Rather complicated (ZIP is multi-file container) ◦ Represented by ZipArchive class  Zlib – gzip library ( *.gz ) ◦ Similar functions to standard C-like API  gzopen(), gzread(), gzwrite(), gzclose(), …  BZip Library ( *.bz2 ) ◦ Similar to Zlib ( bzopen(), …)  Other libraries are available (RAR, LZF, …) ◦ TAR only as PECL extension 19. 3. 2015 by Martin Kruliš (v1.0)7

8 Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)8 When confronted with a problem, some people thinks: “Hey, I’ll use XML!” Then they have two problems…

9  Parsing Process ◦ Loading XML from text file into memory structures  Simple API for XML (SAX) ◦ The document is processed sequentially ◦ Important “events” are reported to the user  Opening/closing an element, attribute found, …  Document Object Model (DOM) ◦ Tree-based structure with object oriented API ◦ Nodes of the tree represent elements, attributes, text content, …. ◦ More suitable for mutable documents 19. 3. 2015 by Martin Kruliš (v1.0)9

10  XPath Query Language ◦ Inspired by filesys. paths (elements ~ directories) ◦ Various types of queries that yield  Boolean, number, string, or set of DOM nodes ◦ Quite expressive (num. operations, conditions, …)  XSLT Transformations ◦ XSLT describes transformation of one XML document to another (or to HTML, or to plain-text)  XSLT rules are stored in XML document ◦ Uses XPath for search queries ◦ Turing complete language 19. 3. 2015 by Martin Kruliš (v1.0)10

11  XML Utilities in PHP ◦ SAX parser ◦ DOM API and parser ◦ XPath query engine (over a DOM API) ◦ XSLT processor ◦ PHP-specific Simple XML API (simplified DOM)  Most of the API is object oriented ◦ DOM is object oriented by design ◦ Simple XML is based on specific properties of PHP classes (e.g., iterators or magic methods) 19. 3. 2015 by Martin Kruliš (v1.0)11

12  Event Driven Parsing ◦ The document is processed as a stream  In a single call of xml_parse() ◦ Important encounters are reported via callbacks  xml_set_element_handler()  xml_set_character_handler()  … 19. 3. 2015 by Martin Kruliš (v1.0)12 Example 1

13  Document Object Model in PHP ◦ Set of PHP-native classes ◦ Implementing DOM 3 as defined by W3C  DOMDocument – entire XML document  DOMElement – tree node of one XML element  DOMAttr – attribute of an element  DOMNode – base class for all tree classes  DOMCharacterData – plain text contents  … 19. 3. 2015 by Martin Kruliš (v1.0)13

14  Document Validation ◦ DOMDocument methods for validation  validate(), schemaValidate()  Automatic validation when the document is parsed  If validateOnParse == true  DOMXPath Class for XPath Search ◦ Operates on DOMDocument ◦ Search is performed by query() or evaluate() ◦ The result is either basic PHP type ( boolean, float, or string ) or DOMNodeList object 19. 3. 2015 by Martin Kruliš (v1.0)14 Example 2

15  XSLTProcessor Class ◦ Uses libxslt external library ◦ Transformation rules are loaded from a DOM document or from a Simple XML Element ◦ Transformation input is a DOMDocument object ◦ The result is yielded  As a new DOMDocument object  String with serialized XML/HTML/text document  Directly to a file 19. 3. 2015 by Martin Kruliš (v1.0)15 Example 3

16  Simplified DOM API ◦ Also loads the document into tree structure ◦ All nodes are SimpleXMLElement objects  Tree traversal is implemented by overloading class iterators and magic methods __get(), __set(), … ◦ The structure can be modified and saved ◦ Some advanced functions are also available  Integrated XPath query processor  Conversions to/from DOM structure 19. 3. 2015 by Martin Kruliš (v1.0)16 Example 4

17  JavaScript Object Notation (JSON) ◦ Lightweight interchange format for structured data ◦ Based on subset of JavaScript language ◦ Otherwise language independent  Many parsers exist with frontends for many languages ◦ Intended for replacing XML in simple scenarios  Syntax ◦ Two basic structures: collections and lists ◦ Supports strings, numbers, bools, and null type ◦ Unicode safe 19. 3. 2015 by Martin Kruliš (v1.0)17

18  JSON Example [ { "StudentId": 42, "Name": "John Smith" }, { "StudentId": 54, "Name": "Jane Johnson", "Graduated": true } ] 19. 3. 2015 by Martin Kruliš (v1.0)18 Ordered list Named collection Number (int) Unicode string Boolean literal

19  JSON Functions ◦ json_decode() – JSON string -> object/array struct. ◦ json_encode() – serialize any PHP type (except resource) into JSON string ◦ json_last_error(), json_last_error_msg()  Customizing Serialization Process ◦ JsonSerializable interface ◦ Abstract method jsonSerialize() that converts object into anything that can be processed by json_encode() 19. 3. 2015 by Martin Kruliš (v1.0)19

20 Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)20

21  PHP: Hypertext Preprocessor ◦ Not restricted only to (hyper)text ◦ HTTP with MIME can accommodate any content  Compressed packages (zip, tar.gz, …)  Application documents (PDF, …)  Images  Caveats ◦ PHP is text oriented  Binary data are represented as strings ◦ PHP is interpreted  Processing binary data might be slow 19. 3. 2015 by Martin Kruliš (v1.0)21

22  GD Library ◦ The php_gd2 module ◦ Supports various formats (JPEG, GIF, PNG, …)  Working with Images ◦ Image is a resource $image = imagecreate(400, 300); ◦ Large variety of functions for manipulation ◦ Image is not compressed in internal representation ◦ Can be saved or written to output  imagepng(), imagejpg(), imagegif()  Corresponding MIME type needs to be set 19. 3. 2015 by Martin Kruliš (v1.0)22

23  Functions ◦ Loading/saving supported formats ◦ Basic pixel operations ◦ Drawing simple geometric shapes  Lines, circles, polygons, … ◦ Text rendering  Built-in fonts, PostScript fonts, TrueType fonts ◦ Transformation and resampling ◦ Copying, alpha blending  Functions are version specific ◦ And time/memory demanding 19. 3. 2015 by Martin Kruliš (v1.0)23 Example 5

24  Exchangeable Image File Format ◦ Metadata for photographs ◦ Extension of JPEG and TIFF containers  PHP API ◦ Read only functions ◦ exif_imagetype() – returns image type ◦ exif_read_data() – reads meta-data from image ◦ exif_thumbnail() – returns the image thumbnail (if present) as binary string 19. 3. 2015 by Martin Kruliš (v1.0)24 Example 6

25  Cairo ◦ Fast 2D drawing library written in C ◦ Supports transformations and Bezier curves  Gmagic ◦ A Swiss army knife for image manipulation ◦ Large variety of supported formats  ImageMagic ◦ Well established library for image manipulation ◦ Designed even for more complex operations 19. 3. 2015 by Martin Kruliš (v1.0)25

26 Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)26

27  MySQL Revision ◦ Original mysql API is deprecated (as of PHP 5.5) ◦ MySQL Improved (mysqli) API  Dual object/procedural interface  Procedural interface is similar to original (deprecated) API  Advanced connectivity features  Persistent connections, compression, encryption  Directly supports transactions ◦ MySQL Native Driver (mysqlnd) extension  More direct access to MySQL server  Additional features (e.g., asynchronous queries) 19. 3. 2015 by Martin Kruliš (v1.0)27

28  MySQLi API ◦ mysqli class – the connection to the DBMS  Manage controls, settings, info, stats, …  Issue queries and multi-queries  Performs transaction control ◦ mysqli_stmt class – SQL statement representation  SQL statement preparation and configuration  Argument bindings ◦ mysqli_result class – SELECT result wrapper  Various methods for accessing data in the result 19. 3. 2015 by Martin Kruliš (v1.0)28

29  Extensions ◦ mysqlnd_qc  Transparent cache for MySQL queries ◦ mysqlnd_memcache  Special extension that utilizes InnoDB Memcache  Translates SQL statements to Memcache protocol ◦ mysqlnd_uh  Allows user to insert hooks for low-level calls  Can be used for monitoring or auditing ◦ mysqlnd_mux  Transparent client-side connection multiplexing 19. 3. 2015 by Martin Kruliš (v1.0)29

30  PostrgreSQL ◦ Quite powerful alternative for MySQL ◦ Similar API to MySQL  pg_connect(), pg_query(), …  SQLite ◦ SQL engine running directly on filesystem ◦ Small project, where regular DBMS is not available  Commercial Giants ◦ MSSQL, Oracle, dBase, IBM DB2, … 19. 3. 2015 by Martin Kruliš (v1.0)30

31  Abstract Database Layers ◦ Solving the problem of portability  If the application refrains from using specific SQL statements  PHP Data Objects (PDO) ◦ Generic interface for various RDBMS ◦ Extension php_pdo, individual DB systems are in separate extensions ( php_pdo_mysql, …) ◦ Simple object oriented API  PDO and PDOStatement classes  Minimalistic, no special functions 19. 3. 2015 by Martin Kruliš (v1.0)31

32 19. 3. 2015 by Martin Kruliš (v1.0)32


Download ppt "Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1."

Similar presentations


Ads by Google