PHP Programming: Difference between revisions

From Training Material
Jump to navigation Jump to search
 
(59 intermediate revisions by the same user not shown)
Line 10: Line 10:


== Intro ==
== Intro ==
'''PHP'''
* general-purpose '''scripting''' language  
* general-purpose '''scripting''' language  
* especially suited to '''web''' development
* especially suited to '''web''' development
Line 15: Line 16:
* powers everything from simple '''blog''' to the '''corporate''' organisations
* powers everything from simple '''blog''' to the '''corporate''' organisations


== Data Structures (refresher only) ==
=== Intro con't ===
'''PHP''' - recursive acronym for ''PHP: Hypertext Preprocessor''
* widely-used '''open source''' language
* can be '''embedded''' into '''HTML'''
* code is executed '''on the server''', generating HTML which is then sent to the client
<!-- The client would receive the results of running that script, but would not know what the underlying code was. -->
* extremely '''simple''', yet offers many advanced '''features'''
 
=== PHP can ===
* collect '''form''' data
* generate '''dynamic page''' content
* send and receive '''cookies'''
* so in general - '''server-side''' scripting
** whats needed? - the '''PHP parser''' (CGI or server module), a '''web server''', and a '''web browser'''
* be used on all major operating systems
* '''support''' most of the '''web servers''' (Apache, IIS, lighttpd, nginx, etc)
* support '''talking to''' other '''services''' - ''LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (on Windows)''
* work as either a '''module''', or as a '''CGI processor'''
* do '''procedural''' programming or '''object-oriented''' programming (OOP), or a mixture of them '''both'''


Associative arrays
=== PHP can con't ===
Session variables, global variables, cookies
* also do '''command line''' scripting
Date
** '''run without''' any server or browser - only the PHP '''parser''' is needed
File and File Upload
** ideal for scripts regularly executed using '''cron''' (on ''Unix'' or ''macOS'') or '''Task Scheduler''' (on ''Windows'')
Cookies and Sessions
* so ie. do simple/stronger '''text processing''' tasks - ''Perl'' regexps (''PCRE''), etc
E-mail
* output '''HTML''', images, '''PDF''', '''encrypted''' data, emails, '''JSON''', XML (''libxml2, SimpleXML, XMLReader, XMLWriter; XDebug'')
Error Handling
** '''auto generate''' these files, and '''save''' them in the file system, instead of printing it out, forming a '''server-side cache''' for dynamic content
Exceptions Handling
* support a wide range of '''databases'''
PHP Filter
** via db specific ext, or abstraction layers (like ''PDO''), or ODBC, cURL, '''sockets''' (''CouchDB'')
== Security ==
* support the '''WDDX''' complex '''data exchange'''
* '''instantiate Java''' objects and use them '''transparently''' as own objects


authorization and authentication
== Data Structures (refresher only) ==
using cookies and sessions
* Associative arrays
== Dealing with Images ==
* Session variables, global variables, cookies
* Date
* File and File Upload
* Cookies and Sessions
* E-mail
* Error Handling
* Exceptions Handling
* PHP Filter
* '''Helpers'''
** <small>https://phpcheatsheets.com/</small>
** <small>https://phptherightway.com/pages/The-Basics.html</small>
'''Examples and exercises'''
* ''localhost/phpWay''


GD Library and alternatives
== PHP XML ==
== PHP XML ==
* SimpleXML
* '''Expat''' Parser - event-based
** https://www.php.net/manual/en/ref.xml.php
* XML DOM
* '''XML Reader'''
** https://www.php.net/XMLReader
** '''iterators''' helper - https://github.com/hakre/XMLReaderIterator
'''Examples and exercises'''
* ''localhost/ws_''


SimpleXML
Expat Parser
XML DOM
== Object-Oriented Programming ==
Terminology Object
Declaring classes and constructors
Creating and Using Objects
Properties and methods
Inheritance
Access modifiers
Magic methods
Declaring and using interfaces
exception Handling
== Using Databases ==
== Using Databases ==
* Methods of data storing
* PDO extension
* Connecting to a database using PDO
* Operations on data
* Error Handling PDO
* Transactions
* Primary keys
* Joins
* Aggregate functions and grouping
* connect to the database
* storing passwords
* executing queries
* processing data
* modifying and removing data
* binding form to records in the database
* '''Helpers'''
** <small>https://phptherightway.com/#databases</small>
'''Examples and exercises'''
* ''localhost/phpWay''
* ''localhost/ws_''
* ''localhost/wordpress''


Methods of data storing data
PDO extension
Connecting to a database using PDO
Operations on data
Error Handling PDO
Transactions
Primary keys
Joins
Aggregate functions and grouping
connect to the database
storing passwords
executing queries
processing data
modifying and removing data
binding form to records in the database
== Programming interfaces ==
== Programming interfaces ==
* Architecture services
* Adding to the system API
* The data format JSON and XML
* RESTful Services
* Designing Web Service
'''Examples and exercises'''
* [[REST_API#Exercises|WebServices]]
* ''localhost/ws_''
== Object-Oriented Programming ==
* Terminology Object
* Declaring classes and constructors
* Creating and Using Objects
* Properties and methods
* Inheritance
* Access modifiers
* Magic methods
* Declaring and using interfaces
* exception Handling
* '''Helpers'''
** <small>https://phptherightway.com/#programming_paradigms</small>
'''Examples and exercises'''
* ''localhost/ws_''
* ''localhost/wordpress''
== Dealing with Images ==
* GD Library and alternatives
** '''GD''' - <small>https://www.php.net/manual/en/book.image.php</small>
** '''ImageMagick''' - <small>https://www.php.net/manual/en/book.imagick.php</small>
** '''Gmagic''' - <small>https://www.php.net/manual/en/book.gmagick.php</small>
* '''Exif''' - image meta data only
** <small>https://www.php.net/manual/en/book.exif.php</small>
'''Examples and Exercises'''
* ''localhost/mw'' - enable GD and check how mw uses Image thumbnailing


Architecture services
== Design Patterns, PSRs ==
Adding to the system API
* What are design patterns
The data format JSON and XML
* Singleton Pattern
RESTful Services
* Factory Pattern
Designing Web Service
* MVC model-view-controller
== Design Patterns ==
* '''Helpers'''
** <small>https://www.php-fig.org/psr/</small>
** <small>https://phptherightway.com/#design_patterns</small>
'''Examples and exercises'''
* ''localhost/ws_''
* ''localhost/wordpress''
<!-- TODO: prep the OOP example with tdd, unit tests and docker; DAY2 -->


What are design patterns
Singleton Pattern
Factory Pattern
MVC model-view-controller
== Efficiency ==
== Efficiency ==
* Research applications performance
* Code cache
** built-in '''opcache''' (since ver 5)
** '''APCu''', Memcached, Redis, WinCache
* '''Helpers'''
** <small>https://phptherightway.com/#opcode_cache</small>
'''Examples and exercises'''
* ''localhost/wm'' - install '''wm''' and enable APCu


Research applications performance
Code cache
== Safety ==
== Safety ==
* Filtering input
* Session fixation
* Session hijacking
* SQL injection
* Storing passwords
* SSL
== Security ==
* authorization and authentication
* using cookies and sessions
* '''OAUTH''' - <small>https://oauth.net/2/</small>
** ''openid'' - <small>https://openid.net/developers/how-connect-works/</small>
* '''JWT''' - <small>https://www.rfc-editor.org/info/rfc7519</small>
** ''Demo'' - <small>https://jwt.io/</small>
** ''Popular lib'' - <small>https://github.com/firebase/php-jwt</small>
== Stacks and Frameworks - lamp, wamp, etc ==
* '''wordpress'''
** <small>https://developer.wordpress.org/advanced-administration/before-install/development/</small>
* '''drupal'''
** <small>https://www.drupal.org/docs/official_docs/local-development-guide</small>
* '''symfony'''
** <small>https://symfony.com/doc/current/setup.html</small>
* '''Laravel''': A PHP framework known for its clean architecture and robust features, including database agnostic migrations, ORM, routing, and authentication tools.
** <small>https://laravel.com/docs/12.x#installing-php</small>
* '''Zend Framework''': An open-source framework for developing web applications and services using PHP.
** <small>https://docs.laminas.dev/laminas-mvc/</small>
<!--
* '''CakePHP''': A PHP framework that offers a robust set of tools for web application development.
** todo
* '''PSX''': A PHP framework dedicated to building fully typed REST APIs, offering a new approach to API development and challenging existing frameworks like Laravel and Symfony.
** todo
* '''yannoff/console''': A lightweight, simple alternative to symfony/console designed for easy PHP applications development.
** todo
-->
=== wp ===
* '''wp-env'''
** <small>https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env/</small>
* '''ddev'''
** <small>https://ddev.readthedocs.io/en/stable/users/quickstart/#wordpress</small>
* '''vvv'''
** <small>https://varyingvagrantvagrants.org/</small>
* '''xampp'''
** <small>http://apachefriends.org/</small>
* '''mamp'''
** <small>https://documentation.mamp.info/en/MAMP-Windows/index.html</small>
* '''local'''
** <small>https://localwp.com/</small>
=== wp con't ===
wp '''API''' refs
* <small>https://developer.wordpress.org/reference/</small>


Filtering input
=== mw ===
Session fixation
* Docs
Session hijacking
** Main <small>https://www.mediawiki.org/wiki/Manual:Contents</small>
SQL injection
** Code <small>https://doc.wikimedia.org/mediawiki-core/master/php/</small>
Storing passwords
* Stacks <small>https://www.mediawiki.org/wiki/Software_bundles</small>
SSL

Latest revision as of 13:31, 18 March 2025

THIS IS A DRAFT

This text may not be complete.

PHP Programming Training Materials

IMPORTANT NOTE for trainers only: 
Please DO NOT change these materials, especially the EXERCISES, without direct permission from: LUKASZ SOKOLOWSKI.

Intro

PHP

  • general-purpose scripting language
  • especially suited to web development
  • fast, flexible and pragmatic
  • powers everything from simple blog to the corporate organisations

Intro con't

PHP - recursive acronym for PHP: Hypertext Preprocessor

  • widely-used open source language
  • can be embedded into HTML
  • code is executed on the server, generating HTML which is then sent to the client
  • extremely simple, yet offers many advanced features

PHP can

  • collect form data
  • generate dynamic page content
  • send and receive cookies
  • so in general - server-side scripting
    • whats needed? - the PHP parser (CGI or server module), a web server, and a web browser
  • be used on all major operating systems
  • support most of the web servers (Apache, IIS, lighttpd, nginx, etc)
  • support talking to other services - LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (on Windows)
  • work as either a module, or as a CGI processor
  • do procedural programming or object-oriented programming (OOP), or a mixture of them both

PHP can con't

  • also do command line scripting
    • run without any server or browser - only the PHP parser is needed
    • ideal for scripts regularly executed using cron (on Unix or macOS) or Task Scheduler (on Windows)
  • so ie. do simple/stronger text processing tasks - Perl regexps (PCRE), etc
  • output HTML, images, PDF, encrypted data, emails, JSON, XML (libxml2, SimpleXML, XMLReader, XMLWriter; XDebug)
    • auto generate these files, and save them in the file system, instead of printing it out, forming a server-side cache for dynamic content
  • support a wide range of databases
    • via db specific ext, or abstraction layers (like PDO), or ODBC, cURL, sockets (CouchDB)
  • support the WDDX complex data exchange
  • instantiate Java objects and use them transparently as own objects

Data Structures (refresher only)

Examples and exercises

  • localhost/phpWay

PHP XML

Examples and exercises

  • localhost/ws_

Using Databases

  • Methods of data storing
  • PDO extension
  • Connecting to a database using PDO
  • Operations on data
  • Error Handling PDO
  • Transactions
  • Primary keys
  • Joins
  • Aggregate functions and grouping
  • connect to the database
  • storing passwords
  • executing queries
  • processing data
  • modifying and removing data
  • binding form to records in the database
  • Helpers

Examples and exercises

  • localhost/phpWay
  • localhost/ws_
  • localhost/wordpress

Programming interfaces

  • Architecture services
  • Adding to the system API
  • The data format JSON and XML
  • RESTful Services
  • Designing Web Service

Examples and exercises

Object-Oriented Programming

  • Terminology Object
  • Declaring classes and constructors
  • Creating and Using Objects
  • Properties and methods
  • Inheritance
  • Access modifiers
  • Magic methods
  • Declaring and using interfaces
  • exception Handling
  • Helpers

Examples and exercises

  • localhost/ws_
  • localhost/wordpress

Dealing with Images

Examples and Exercises

  • localhost/mw - enable GD and check how mw uses Image thumbnailing

Design Patterns, PSRs

Examples and exercises

  • localhost/ws_
  • localhost/wordpress

Efficiency

Examples and exercises

  • localhost/wm - install wm and enable APCu

Safety

  • Filtering input
  • Session fixation
  • Session hijacking
  • SQL injection
  • Storing passwords
  • SSL

Security

Stacks and Frameworks - lamp, wamp, etc

wp

wp con't

wp API refs

mw