SlideShare a Scribd company logo
Modern PHP
Charles Anderson

Hack Salem! - July 2019
TL;DR
• PHP has grown a lot over the last 15 - 20 years

• duh!

• It’s much more reasonable for larger projects
Me
• I’m a systems programmer -
code w/o a face

• I am not really a PHP
programmer

• Ruby, Puppet, bash, etc.

• I did some PHP back in the
early Naughties

• CGI and bare Servlets before
that Gorak aka Steve aka Larry
Acquia
• Acquia provides enterprise-grade Drupal CMS hosting

• Lots more, too

• Operate a fleet of tens of thousands of AWS servers

• Controlled with a mix of PHP, Ruby, Puppet, etc.

• Acquia is hiring
PHP 4 and Before
• PHP started as a simple way to add some computation to
web pages

• The simplest (lamest) example was a visitor counter
Gotta Have a Counter
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<p>Hello world</p>
<?php
$path = './counter.txt';
$file = fopen($path, 'r');
$count = fgets($file, 1000);
fclose($file);
$count = abs(intval($count)) + 1;
?>
<p>Congratulations: you are visitor
number:
<b> <?php echo "{$count}n"; ?> </b>
</p>
<?php
$file = fopen($path, 'w');
fwrite($file, $count);
fclose($file);
?>
</body>
</html>
HTML
HTML
PHP Business Logic
HTML
PHP Presentation Logic
PHP Business Logic
PHP
• A simple mix of HTML and business logic - what could
possibly go wrong?

• Global functions and variables (unless local in a function)
Global Libraries
PHP Language
• Variables - $name
• Dynamic typing: $name=‘Damien’; $name=666;

• Scalar types: boolean, integer, double, string

• Arrays: always hashes - array[1] or array[‘name’]

• Flow control: if, switch, for, foreach, while

• Including code: include or require
Classes
• PHP 4 added classes

• Methods and properties - all public

• $object->method() or $object->property

• Single inheritance with extends keyword

• Introspection - classes, methods, properties, parent class

• Example: PEAR database library to abstract underlying
DB
PHP 7.2
Objects and Classes
• Visibility - public, protected, private

• Interfaces - a list of methods that must be implemented

• Abstract classes - implement a subset of methods of an
interface

• Traits - a collection of method implementations to include
in a class without inheritance - mix-ins

• Basically, the kitchen sink
Visibility
class Politician {
public function promises() {}
protected function donors() {}
private function scandals() {}
private $slushFundBalance = 1000000;
}
Interfaces
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
}
class Template implements iTemplate
{
private $vars = array();
public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
public function getHtml($template)
{
foreach($this->vars as $name => $value) {
$template = str_replace('{' . $name . '}', $value, $template);
}
return $template;
}
}
Abstract Classes
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
}
// no getHtml method - must be abstract
abstract class SetTemplate implements iTemplate
{
private $vars = array();
public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
}
Traits
trait Logger {
function log($msg) {
echo '(' . __CLASS__ .  ') ' . $msg ;
}
}
class BankAccount {
use Logger;
function withdraw($amount) {
$this->log('withdraw ' . $amount);
}
...
}
class Database {
use Logger;
function query($sql) {
$this->log('query ' .$sql);
}
...
}
Type Hints
• Have the option (not required) to specify types on
parameters, properties, and return values

• If a type is specified, you must (strictly) abide

• Nullable types - e.g., a function could return Foo or NULL

• Variables can still be dynamically typed

• Provides a path to slowly introduce typing to an existing
body of software
Type Hints
function sum(int $a, int $b) {
return $a + $b;
}
$result = sum(3, 4); // OK
$result = sum(3, 'cats') //FAIL
$result = sum(3.5, 4); // OK - really?
// another file
declare(strict_types=1);
$result = sum(3.5, 4); // FAIL
Nullabe Types
function welcome(?string $name) {
echo $name;
}
welcome('world'); // OK
welcome(null); // OK
welcome(); // FAIL
function welcome($name): ?string
{
return null; // OK
}
function welcome($name): ?string
{
return 'Welcome ' . $name; // OK
}
function welcome($name): ?string
{
return 33; // FAIL
}
Large Scale Software
• Namespaces - no longer only a global, flat namespace

• Packaging / Composer: install libraries and manage
dependencies
Namespaces
<?php
namespace MyApplication;
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
<?php
namespace ThirdPartyDatabaseUtil;
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
$status = CONNECT_OK; // local namespace
$status = MyApplicationCONNECT_OK; // other namespace
Top-Level Namespace
Sub-Namespace
Packages
• PHP code can be packaged into a reusable collection
called simply a package - c.f., Ruby gems

• The Composer tool is a per-project package manager that
fetches the packages you need and all of the
dependencies - c.f., Ruby Bundler
Composer
• In your project directory, you can run

composer require "twig/twig:^2.0"

• Creates composer.json and composer.lock files and
vendor directory

• composer install - install all the packages

• To use the packages, include one line of code:

require_once __DIR__ . ‘/vendor/autoload.php';
• Auto-loading loads all the packages at runtime
Other Items
• Exceptions - code without checking return values

• Testing frameworks - phpUnit and Behat (BDD)

• Style/lint checkers - phpstan and phpcs 

• see also John’s talk
Conclusions
• PHP has grown up a lot in the last 15 years - definitely no
longer Stone Age

• You can easily write large, well structured applications

• You can still write a mess of logic and presentation - it’s
a free country

More Related Content

What's hot

Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitd
Sorabh Jain
 
Php hacku
Php hackuPhp hacku
Phinx talk
Phinx talkPhinx talk
Phinx talk
Michael Peacock
 
Using PHP
Using PHPUsing PHP
Using PHP
Mark Casias
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojo
bpmedley
 
Keeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro frameworkKeeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro framework
Jeremy Kendall
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
diego_k
 
An Introduction to Symfony
An Introduction to SymfonyAn Introduction to Symfony
An Introduction to Symfony
xopn
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
Jeremy Kendall
 
Php mysql
Php mysqlPhp mysql
Php mysql
Manish Jain
 
Mojo as a_client
Mojo as a_clientMojo as a_client
Mojo as a_client
Marcus Ramberg
 
16.mysql stored procedures in laravel
16.mysql stored procedures in laravel16.mysql stored procedures in laravel
16.mysql stored procedures in laravel
Razvan Raducanu, PhD
 
Php converted pdf
Php converted pdfPhp converted pdf
Php converted pdf
Northpole Web Service
 
Ppt php
Ppt phpPpt php
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
Kris Wallsmith
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011
Jacopo Romei
 
コードの動的生成のお話
コードの動的生成のお話コードの動的生成のお話
コードの動的生成のお話
鉄次 尾形
 
Php Training Workshop by Vtips
Php Training Workshop by VtipsPhp Training Workshop by Vtips
Php Training Workshop by Vtips
Vidya Topa Institute of Professional Studies
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
Yusuke Wada
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
webhostingguy
 

What's hot (20)

Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitd
 
Php hacku
Php hackuPhp hacku
Php hacku
 
Phinx talk
Phinx talkPhinx talk
Phinx talk
 
Using PHP
Using PHPUsing PHP
Using PHP
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojo
 
Keeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro frameworkKeeping it small: Getting to know the Slim micro framework
Keeping it small: Getting to know the Slim micro framework
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
An Introduction to Symfony
An Introduction to SymfonyAn Introduction to Symfony
An Introduction to Symfony
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Mojo as a_client
Mojo as a_clientMojo as a_client
Mojo as a_client
 
16.mysql stored procedures in laravel
16.mysql stored procedures in laravel16.mysql stored procedures in laravel
16.mysql stored procedures in laravel
 
Php converted pdf
Php converted pdfPhp converted pdf
Php converted pdf
 
Ppt php
Ppt phpPpt php
Ppt php
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011
 
コードの動的生成のお話
コードの動的生成のお話コードの動的生成のお話
コードの動的生成のお話
 
Php Training Workshop by Vtips
Php Training Workshop by VtipsPhp Training Workshop by Vtips
Php Training Workshop by Vtips
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
 

Similar to Modern php

Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018
Adam Tomat
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.js
souridatta
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
machuga
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
rICh morrow
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
Darren Craig
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
Iftekhar Eather
 
Modernizing Legacy Applications in PHP, por Paul Jones
Modernizing Legacy Applications in PHP, por Paul JonesModernizing Legacy Applications in PHP, por Paul Jones
Modernizing Legacy Applications in PHP, por Paul Jones
iMasters
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
Perl Careers
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3
Nate Abele
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
Hisateru Tanaka
 
Phpspec tips&amp;tricks
Phpspec tips&amp;tricksPhpspec tips&amp;tricks
Phpspec tips&amp;tricks
Filip Golonka
 
Fatc
FatcFatc
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
Muhamad Al Imran
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 
Introducing ruby on rails
Introducing ruby on railsIntroducing ruby on rails
Introducing ruby on rails
Priceen
 
Starting Out With PHP
Starting Out With PHPStarting Out With PHP
Starting Out With PHP
Mark Niebergall
 
Synapse india reviews on php website development
Synapse india reviews on php website developmentSynapse india reviews on php website development
Synapse india reviews on php website development
saritasingh19866
 
PHP: The easiest language to learn.
PHP: The easiest language to learn.PHP: The easiest language to learn.
PHP: The easiest language to learn.
Binny V A
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHP
Nick Belhomme
 

Similar to Modern php (20)

Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.js
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
Modernizing Legacy Applications in PHP, por Paul Jones
Modernizing Legacy Applications in PHP, por Paul JonesModernizing Legacy Applications in PHP, por Paul Jones
Modernizing Legacy Applications in PHP, por Paul Jones
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
Phpspec tips&amp;tricks
Phpspec tips&amp;tricksPhpspec tips&amp;tricks
Phpspec tips&amp;tricks
 
Fatc
FatcFatc
Fatc
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Introducing ruby on rails
Introducing ruby on railsIntroducing ruby on rails
Introducing ruby on rails
 
Starting Out With PHP
Starting Out With PHPStarting Out With PHP
Starting Out With PHP
 
Synapse india reviews on php website development
Synapse india reviews on php website developmentSynapse india reviews on php website development
Synapse india reviews on php website development
 
PHP: The easiest language to learn.
PHP: The easiest language to learn.PHP: The easiest language to learn.
PHP: The easiest language to learn.
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHP
 

More from Charles Anderson

Literate Programming
Literate ProgrammingLiterate Programming
Literate Programming
Charles Anderson
 
Inrastructure as Code
Inrastructure as CodeInrastructure as Code
Inrastructure as Code
Charles Anderson
 
How to get a Software Job w/o Experience
How to get a Software Job w/o ExperienceHow to get a Software Job w/o Experience
How to get a Software Job w/o Experience
Charles Anderson
 
Docker - Hack Salem! - November 2014
Docker - Hack Salem! - November 2014Docker - Hack Salem! - November 2014
Docker - Hack Salem! - November 2014
Charles Anderson
 
A Brief Introduction to Redis
A Brief Introduction to RedisA Brief Introduction to Redis
A Brief Introduction to Redis
Charles Anderson
 
A Shallow Survey of Alternative Languages on the JVM
A Shallow Survey of Alternative Languages on the JVMA Shallow Survey of Alternative Languages on the JVM
A Shallow Survey of Alternative Languages on the JVM
Charles Anderson
 
How to Get a Software Job w/o Experience
How to Get a Software Job w/o ExperienceHow to Get a Software Job w/o Experience
How to Get a Software Job w/o Experience
Charles Anderson
 
How To Protect Yourself and Your Computer Online
How To Protect Yourself and Your Computer OnlineHow To Protect Yourself and Your Computer Online
How To Protect Yourself and Your Computer Online
Charles Anderson
 
Jython: Integrating Python and Java
Jython: Integrating Python and JavaJython: Integrating Python and Java
Jython: Integrating Python and Java
Charles Anderson
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
Charles Anderson
 

More from Charles Anderson (10)

Literate Programming
Literate ProgrammingLiterate Programming
Literate Programming
 
Inrastructure as Code
Inrastructure as CodeInrastructure as Code
Inrastructure as Code
 
How to get a Software Job w/o Experience
How to get a Software Job w/o ExperienceHow to get a Software Job w/o Experience
How to get a Software Job w/o Experience
 
Docker - Hack Salem! - November 2014
Docker - Hack Salem! - November 2014Docker - Hack Salem! - November 2014
Docker - Hack Salem! - November 2014
 
A Brief Introduction to Redis
A Brief Introduction to RedisA Brief Introduction to Redis
A Brief Introduction to Redis
 
A Shallow Survey of Alternative Languages on the JVM
A Shallow Survey of Alternative Languages on the JVMA Shallow Survey of Alternative Languages on the JVM
A Shallow Survey of Alternative Languages on the JVM
 
How to Get a Software Job w/o Experience
How to Get a Software Job w/o ExperienceHow to Get a Software Job w/o Experience
How to Get a Software Job w/o Experience
 
How To Protect Yourself and Your Computer Online
How To Protect Yourself and Your Computer OnlineHow To Protect Yourself and Your Computer Online
How To Protect Yourself and Your Computer Online
 
Jython: Integrating Python and Java
Jython: Integrating Python and JavaJython: Integrating Python and Java
Jython: Integrating Python and Java
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
 

Recently uploaded

Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
Ayan Halder
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
mz5nrf0n
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 

Recently uploaded (20)

Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 

Modern php

  • 2. TL;DR • PHP has grown a lot over the last 15 - 20 years • duh! • It’s much more reasonable for larger projects
  • 3. Me • I’m a systems programmer - code w/o a face • I am not really a PHP programmer • Ruby, Puppet, bash, etc. • I did some PHP back in the early Naughties • CGI and bare Servlets before that Gorak aka Steve aka Larry
  • 4.
  • 5. Acquia • Acquia provides enterprise-grade Drupal CMS hosting • Lots more, too • Operate a fleet of tens of thousands of AWS servers • Controlled with a mix of PHP, Ruby, Puppet, etc. • Acquia is hiring
  • 6. PHP 4 and Before • PHP started as a simple way to add some computation to web pages • The simplest (lamest) example was a visitor counter
  • 7. Gotta Have a Counter <!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <p>Hello world</p> <?php $path = './counter.txt'; $file = fopen($path, 'r'); $count = fgets($file, 1000); fclose($file); $count = abs(intval($count)) + 1; ?> <p>Congratulations: you are visitor number: <b> <?php echo "{$count}n"; ?> </b> </p> <?php $file = fopen($path, 'w'); fwrite($file, $count); fclose($file); ?> </body> </html> HTML HTML PHP Business Logic HTML PHP Presentation Logic PHP Business Logic
  • 8. PHP • A simple mix of HTML and business logic - what could possibly go wrong? • Global functions and variables (unless local in a function)
  • 10. PHP Language • Variables - $name • Dynamic typing: $name=‘Damien’; $name=666; • Scalar types: boolean, integer, double, string • Arrays: always hashes - array[1] or array[‘name’] • Flow control: if, switch, for, foreach, while • Including code: include or require
  • 11. Classes • PHP 4 added classes • Methods and properties - all public • $object->method() or $object->property • Single inheritance with extends keyword • Introspection - classes, methods, properties, parent class • Example: PEAR database library to abstract underlying DB
  • 13. Objects and Classes • Visibility - public, protected, private • Interfaces - a list of methods that must be implemented • Abstract classes - implement a subset of methods of an interface • Traits - a collection of method implementations to include in a class without inheritance - mix-ins • Basically, the kitchen sink
  • 14. Visibility class Politician { public function promises() {} protected function donors() {} private function scandals() {} private $slushFundBalance = 1000000; }
  • 15. Interfaces interface iTemplate { public function setVariable($name, $var); public function getHtml($template); } class Template implements iTemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } public function getHtml($template) { foreach($this->vars as $name => $value) { $template = str_replace('{' . $name . '}', $value, $template); } return $template; } }
  • 16. Abstract Classes interface iTemplate { public function setVariable($name, $var); public function getHtml($template); } // no getHtml method - must be abstract abstract class SetTemplate implements iTemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } }
  • 17. Traits trait Logger { function log($msg) { echo '(' . __CLASS__ .  ') ' . $msg ; } } class BankAccount { use Logger; function withdraw($amount) { $this->log('withdraw ' . $amount); } ... } class Database { use Logger; function query($sql) { $this->log('query ' .$sql); } ... }
  • 18. Type Hints • Have the option (not required) to specify types on parameters, properties, and return values • If a type is specified, you must (strictly) abide • Nullable types - e.g., a function could return Foo or NULL • Variables can still be dynamically typed • Provides a path to slowly introduce typing to an existing body of software
  • 19. Type Hints function sum(int $a, int $b) { return $a + $b; } $result = sum(3, 4); // OK $result = sum(3, 'cats') //FAIL $result = sum(3.5, 4); // OK - really? // another file declare(strict_types=1); $result = sum(3.5, 4); // FAIL
  • 20. Nullabe Types function welcome(?string $name) { echo $name; } welcome('world'); // OK welcome(null); // OK welcome(); // FAIL function welcome($name): ?string { return null; // OK } function welcome($name): ?string { return 'Welcome ' . $name; // OK } function welcome($name): ?string { return 33; // FAIL }
  • 21. Large Scale Software • Namespaces - no longer only a global, flat namespace • Packaging / Composer: install libraries and manage dependencies
  • 22. Namespaces <?php namespace MyApplication; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } <?php namespace ThirdPartyDatabaseUtil; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } $status = CONNECT_OK; // local namespace $status = MyApplicationCONNECT_OK; // other namespace Top-Level Namespace Sub-Namespace
  • 23. Packages • PHP code can be packaged into a reusable collection called simply a package - c.f., Ruby gems • The Composer tool is a per-project package manager that fetches the packages you need and all of the dependencies - c.f., Ruby Bundler
  • 24. Composer • In your project directory, you can run
 composer require "twig/twig:^2.0" • Creates composer.json and composer.lock files and vendor directory • composer install - install all the packages • To use the packages, include one line of code:
 require_once __DIR__ . ‘/vendor/autoload.php'; • Auto-loading loads all the packages at runtime
  • 25. Other Items • Exceptions - code without checking return values • Testing frameworks - phpUnit and Behat (BDD) • Style/lint checkers - phpstan and phpcs • see also John’s talk
  • 26. Conclusions • PHP has grown up a lot in the last 15 years - definitely no longer Stone Age • You can easily write large, well structured applications • You can still write a mess of logic and presentation - it’s a free country