ElePHPant7 - Introduction to PHP7

Post on 09-Feb-2017

126 views 0 download

transcript

Assalamualaikum!Muhammad Hafiz HasanUnit Government Online Services (GOS)mhafiz@jpm.gov.myhttps://my.linkedin.com/in/mhafizhasan

Agenda

✓ Introduction✓ Why upgrade✓ Upgrade checklist✓ Upgrade recommendations✓ Getting help

Introduction

Evolution Timeline

PHP 1.0(1995)

Personal Home Page Tools

(PHP)

PHP 2.0(1997)

PHP 3.0(1998)

PHP 4.0(2000)

Introduce Zend Engine

PHP 5.0(2004)

Zend Engine II

PHP 5.1(2005)

PHP Data Objects (PDO)

PHP 5.3(2009)

Namespace support

PHP 5.4(2012)

Trait support

PHP 7.0(2015)

Zend Engine III

source: https://en.wikipedia.org/wiki/PHP

Why UpgradePHP5 to PHP7

Why upgrade? PHP5 to PHP7

PHP 5is now 13 years old

+ PHP 7

is mature enough

Why upgrade? PHP5 to PHP7

Upshift in performance withPHPNG

(PHP Next Generation)

=at par with

Facebook HHVM(HipHop Virtual Machine)

Why upgrade? PHP5 to PHP7

Run up to 3x Magento transactions on the same hardware

With execution time more than twice as fast compared to PHP 5.6 and 30% lower memory consumption - servers running PHP 7 will be able to serve up to 3x as many requests as those running PHP 5.6.

source: http://www.zend.com/en/resources/php7_infographic

Why upgrade? PHP5 to PHP7

Drupal 8 runs 72% faster with PHP 7

source: http://www.zend.com/en/resources/php7_infographic

Why upgrade? PHP5 to PHP7

WordPress Screams on PHP 7. You’ll need less

servers to serve the same amount of users!

One WordPress request on PHP 5.6 executes just under

100M CPU instructions, while PHP 7 only executes 25M to

do the same job.

source: http://www.zend.com/en/resources/php7_infographic

Why upgrade? PHP5 to PHP7

We also tested how various PHP frameworks perform

under PHP 7

source: http://www.zend.com/en/resources/php7_infographic

Why upgrade? PHP5 to PHP7

Significant memory saving

Why upgrade? PHP5 to PHP7

Introduction ofScalar Type Hints & Return Types

(string, int, float, bool)

declare(strict_types=1);

function add(int $a, int $b) {

return $a + $b;}

var_dump(add(1,2));

declare(strict_types=1);

function add(int $a, int $b) : int {

return (string)($a + $b);}

var_dump(add(1,2));

Scalar type hints Return type declaration

Why upgrade? PHP5 to PHP7

Introduction ofSpaceship Operator

<=>

Why upgrade? PHP5 to PHP7

Introduction ofNull Coalesce Operator

??

$name = isset($firstname) ? $firstname : “Guest”;

$name = $firstname ?? “Guest”;

$name = $firstname ?? $username ?? $nickname “Guest”;

Why upgrade? PHP5 to PHP7

Introduction ofAnonymous Classes

class className {

// defined properties and methods

};

$object = new className( 'arguments' );

$object = new class( 'arguments' ) {

// defined properties and methods

};

Upgrade Checklist

Upgrade checklist

☑ Removed functions☑ Removed INI directives

Removed functions : ereg*

ereg* replace with PCRE (Perl Compatible Regular Expression)

✓ preg_filter✓ preg_grep✓ preg_last_error✓ preg_match_all✓ preg_match✓ preg_quote✓ preg_replace_callback_array✓ preg_replace_callback✓ preg_replace✓ preg_split

✘ ereg_replace

✘ ereg

✘ eregi_replace

✘ eregi

✘ split

✘ spliti

✘ sql_regcase

✘ mysql_affected_rows

✘ mysql_client_encoding

✘ mysql_close

✘ mysql_connect

✘ mysql_create_db

✘ mysql_data_seek

✘ mysql_db_name

✘ mysql_db_query

✘ mysql_drop_db

✘ mysql_errno

✘ mysql_error

✘ mysql_escape_string

✘ mysql_fetch_array

✘ mysql_fetch_assoc

✘ mysql_fetch_field

✘ mysql_fetch_lengths

✘ mysql_fetch_object

✘ mysql_fetch_row

Removed functions : mysql_*✘ mysql_field_flags

✘ mysql_field_len

✘ mysql_field_name

✘ mysql_field_seek

✘ mysql_field_table

✘ mysql_field_type

✘ mysql_free_result

✘ mysql_get_client_info

✘ mysql_get_host_info

✘ mysql_get_proto_info

✘ mysql_get_server_info

✘ mysql_info

✘ mysql_insert_id

✘ mysql_list_dbs

✘ mysql_list_fields

✘ mysql_list_processes

✘ mysql_list_tables

✘ mysql_num_fields

✘ mysql_num_rows

✘ mysql_pconnect

✘ mysql_ping

✘ mysql_query

✘ mysql_real_escape_string

✘ mysql_result

✘ mysql_select_db

✘ mysql_set_charset

✘ mysql_stat

✘ mysql_tablename

✘ mysql_thread_id

✘ mysql_unbuffered_query

Removed functions : mysql_*

mysql_* < MySQLi < PDO

PHP5.5.0 PHP7

Why PHP Data Object - PDO

Vendor flexibilityReduced learning curve

Named parameters

Why PHP Data Object - PDO

Vendor flexibility

● PostgreSQL

● SQLite

● MySQL

● Oracle

● ODBC

● MS SQLServer & Azure

● Firebird

● Informix

● IBM DB2

● Sybase

● Cubrid

● 4D

Installation

Ubuntusudo apt-get install php5-mysql

CentOSyum install php-pdo php-mysql

Example : Connection

<?php

// MySQL$dbh = new PDO(“mysql:host=$host;dbname=$dbname, $user, $pass”);

// MSSQL$dbh = new PDO(“mssql:host=$host;dbname=$dbname, $user, $pass”);

// Sybase$dbh = new PDO(“sybase:host=$host;dbname=$dbname, $user, $pass”);

// SQLite$dbh = new PDO(“sqlite:my/database/path/database.db”);

Example : Named placeholders

<?php

// No placeholders$sth = $dbh->prepare(“INSERT INTO users (name, email) VALUES ($name, $email)”);

// Unnamed placeholders$sth = $dbh->prepare(“INSERT INTO users (name, email) VALUES (?, ?)”);$sth->bindParam(1, $name);$sth->bindParam(2, $email);

// Named placeholders$sth = $dbh->prepare(“INSERT INTO users (name, email) VALUES (:name, :email)”);$sth->bindParam(‘:name’, $name);$sth->bindParam(‘:email’, $email);

// Execute$sth->execute();

Removed functions : mssql_*

✘ mssql_bind

✘ mssql_close

✘ mssql_connect

✘ mssql_data_seek

✘ mssql_execute

✘ mssql_fetch_array

✘ mssql_fetch_assoc

✘ mssql_fetch_batch

✘ mssql_fetch_field

✘ mssql_fetch_object

✘ mssql_fetch_row

✘ mssql_field_length

✘ mssql_field_name

✘ mssql_field_seek

✘ mssql_field_type

✘ mssql_free_result

✘ mssql_free_statement

✘ mssql_free_statement

✘ mssql_get_last_message

✘ mssql_guid_string

✘ mssql_init

✘ mssql_min_error_severity

✘ mssql_min_message_severity

✘ mssql_next_result

✘ mssql_num_fields

✘ mssql_num_rows

✘ mssql_pconnect

✘ mssql_query

✘ mssql_result

✘ mssql_rows_affected

✘ mssql_select_db

Removed INI directives : always_populate_raw_post_data

$username = $_POST[‘username’];

echo $username;

$post = file_get_contents(“php://input”);

echo $post[‘username’];

$HTTP_RAW_POST_DATA php://input

Removed INI directives : asp_tags

<%...%> <%=...%>

<script language=”php”>...</script>

<?php...?> <?=...?>

Upgrade recommendations

TL;DRToo Long; Didn’t Read

Framework● Structured code and file organisation● Utilities, libraries & helpers● Design pattern (M-V-C)● Security● Rapid application development● Community support● Fun

Popular PHP frameworks - github

Popular PHP framework - survey

source: https://www.sitepoint.com/best-php-framework-2015-sitepoint-survey-results/

Your best friends

http://www.phptherightway.com/

Last but not least,

Thank youAny questions?