zf2 how arrays will save your project

46
Zend Framework2 How arrays will save your project in it 2 PROFESSIONAL PHP SERVICES

Upload: michelangelo-van-dam

Post on 15-Feb-2017

612 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Zf2   how arrays will save your project

Zend Framework2How arrays will save your project

in it2PROFESSIONAL PHP SERVICES

Page 2: Zf2   how arrays will save your project

Michelangelo van DamPHP Consultant, Community Leader & Trainer

http

s://w

ww.

flick

r.com

/pho

tos/

akra

bat/8

7843

1881

3

Page 3: Zf2   how arrays will save your project

ScheduleIntroduction to ZF2

The array

Challenges in development

Solutions offered in ZF2

More options

Page 4: Zf2   how arrays will save your project

ScheduleIntroduction to ZF2

The array

Challenges in development

Solutions offered in ZF2

More options

Page 5: Zf2   how arrays will save your project
Page 6: Zf2   how arrays will save your project
Page 7: Zf2   how arrays will save your project

“The Array Framework”

Page 8: Zf2   how arrays will save your project

<?php /**  * Zend Framework 2 Configuration Settings  *  */ return array(     'modules' => array(         'Application',         'In2it\\SsoProvider',         'In2it\\Crm\\Dashboard',         'In2it\\Crm\\ContactManager',         'In2it\\Crm\\OrderManager',         'In2it\\Crm\\ProductManager',     ),     'module_listener_options' => array(         'module_paths' => array(             './module',             './vendor'         ),         'config_glob_paths' => array(             '/home/dragonbe/workspace/Totem/config/autoload/{,*.}{global,local}.php'         ),         'config_cache_key' => 'application.config.cache',         'config_cache_enabled' => true,         'module_map_cache_key' => 'application.module.cache',         'module_map_cache_enabled' => true,         'cache_dir' => 'data/cache/'     ) );

Page 9: Zf2   how arrays will save your project

<?phpnamespace In2it\Crm\Dashboard;

use Zend\ModuleManager\Feature\ConfigProviderInterface; use Zend\Mvc\ModuleRouteListener; use Zend\Mvc\MvcEvent;

class Module implements ConfigProviderInterface {     public function getAutoloaderConfig()     {         return array(             'Zend\Loader\ClassMapAutoloader' => array(                 __DIR__ . '/autoload_classmap.php',             ),             'Zend\Loader\StandardAutoloader' => array(                 'namespaces' => array(                     __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,                 ),             ),         );     }

    public function getConfig()     {         return include __DIR__ . '/config/module.config.php';     } }

Page 10: Zf2   how arrays will save your project

http

s://w

ww.

flick

r.com

/pho

tos/

dasp

rid/8

1479

8630

7

Page 11: Zf2   how arrays will save your project

Yaml

XML

INI

CSV PHP

Page 12: Zf2   how arrays will save your project

PHP

Page 13: Zf2   how arrays will save your project

ScheduleIntroduction to ZF2

The array

Challenges in development

Solutions offered in ZF2

More options

Page 14: Zf2   how arrays will save your project

array_change_key_case array_chunk array_column

array_combine array_count_values

array_diff_assoc array_diff_key

array_diff_uassoc array_diff_ukey

array_diff array_fill_keys

array_fill array_filter array_flip

array_intersect_assoc array_intersect_key

array_intersect_uassoc array_intersect_ukey

array_intersect array_key_exists

array_keys array_map

array_merge_recursive array_merge

array_multisort array_pad array_pop

array_product

array_push array_rand

array_reduce array_replace_recursive

array_replace array_reverse array_search

array_shift array_slice

array_splice array_sum

array_udiff_assoc array_udiff_uassoc

array_udiff array_uintersect_assoc array_uintersect_uassoc

array_uintersect array_unique array_unshift array_values

array_walk_recursive array_walk

array arsort asort

compact count

current

each end

extract in_array

key_exists key

krsort ksort list

natcasesort natsort

next pos prev

range reset rsort

shuffle sizeof sort

uasort uksort usort

Page 15: Zf2   how arrays will save your project

Do you know them?

Page 16: Zf2   how arrays will save your project

array_search array_filter

? ? ? ??

? ? ??

??

???

? ? ?

Page 17: Zf2   how arrays will save your project

count array_sum

array_product

? ? ? ??

? ? ??

??

???

? ? ?

Page 18: Zf2   how arrays will save your project

array_diff array_intersect array_merge

? ? ? ??

? ? ??

??

???

? ? ?

Page 19: Zf2   how arrays will save your project

// Let's take one value out of our array $array = ['apple', 'banana', 'chocolate'];

$newArray = []; foreach ($array as $value) {   if ('banana' !== $value) {       $newArray[] = $value;   } } echo implode(', ', $newArray);

// Outputs: apple, chocolate

Page 20: Zf2   how arrays will save your project

<?php

// Let's take one value out of our array $array = ['apple', 'banana', 'chocolate'];

// I keep an ignore list as well $ignore = ['banana'];

// Ready for magic? echo implode(', ', array_diff($array, $ignore));

// Outputs: apple, chocolate

Page 21: Zf2   how arrays will save your project

<?php

// I have one associative array $array = ['a' => 'apple', 'b' => 'banana', 'c' => 'chocolate'];

// And a similar value array $similar = ['banana', 'chocolate'];

// I need to get the keys of similar items from original array $newSimilar = []; foreach ($array as $key => $value) {     if (in_array($value, $similar)) {         $newSimilar[$key] = $value;     } } $similar = $newSimilar; unset ($newSimilar);

var_dump($similar);

/* Outputs: array(2) {   'b' =>   string(6) "banana"   'c' =>   string(9) "chocolate" } */

Page 22: Zf2   how arrays will save your project

<?php

// I have one associative array $array = ['a' => 'apple', 'b' => 'banana', 'c' => 'chocolate'];

// And a similar value array $similar = ['banana', 'chocolate'];

// I need to get the keys of similar items from original array $similar = array_intersect($array, $similar);

var_dump($similar);

/* Outputs: array(2) {   'b' =>   string(6) "banana"   'c' =>   string(9) "chocolate" } */

Page 23: Zf2   how arrays will save your project

One more?

Page 24: Zf2   how arrays will save your project

<?php

// I have one associative array $array = ['a' => 'apple', 'b' => 'banana', 'c' => 'chocolate'];

// I need to find a given value -> 'chocolate' $search = 'chocolate'; $searchResult = []; foreach ($array as $key => $value) {     if ($search === $value) {         $searchResult[$key] = $value;     } } var_dump($searchResult);

/* Outputs: array(1) {   'c' =>   string(9) "chocolate" } */

Page 25: Zf2   how arrays will save your project

<?php

// I have one associative array $array = ['a' => 'apple', 'b' => 'banana', 'c' => 'chocolate'];

// I need to find a given value -> 'chocolate' $search = 'chocolate'; $searchResult = array_filter($array, function ($var) use ($search) {      return $search === $var; }); var_dump($searchResult);

/* Outputs: array(1) {   'c' =>   string(9) "chocolate" } */

Page 26: Zf2   how arrays will save your project

ScheduleIntroduction to ZF2

The array

Challenges in development

Solutions offered in ZF2

More options

Page 27: Zf2   how arrays will save your project

Lots of data

Page 28: Zf2   how arrays will save your project

Lists of data rows

Page 29: Zf2   how arrays will save your project

<?php

$query = "SELECT * FROM `contact` WHERE `age` > ? AND `gender` = ?"; $stmt = $pdo->prepare($query); $stmt->bindParam(1, $cleanAge); $stmt->bindParam(2, $cleanGender); $stmt->execute();

// A resultset of 63,992 entries stored in an array!!! $resultList = $stmt->fetchAll();

Page 30: Zf2   how arrays will save your project

<?php

public function getSelectedContacts($age, $gender) {     $resultSet = $this->tableGateway->select(array(         'age' => $age,         'gender' => $gender,     ));    return $resultSet; }

Page 31: Zf2   how arrays will save your project

Iterators!!!

Page 32: Zf2   how arrays will save your project

Loop with arrays• Data fetching time for 63992 of 250000 records:

2.14 seconds

• Data processing time for 63992 of 250000 records: 7.11 seconds

• Total time for 63992 of 250000 records: 9.25 seconds

• Memory consumption for 63992 of 250000 records: 217.75MB

Page 33: Zf2   how arrays will save your project

Loop with Iterators• Data fetching time for 63992 of 250000 records:

0.92 seconds

• Data processing time for 63992 of 250000 records: 5.57 seconds

• Total time for 63992 of 250000 records: 6.49 seconds

• Memory consumption for 63992 of 250000 records: 0.25MB

Page 34: Zf2   how arrays will save your project

Loop with Iterators• Data fetching time for 63992 of 250000 records:

0.92 seconds

• Data processing time for 63992 of 250000 records: 5.57 seconds

• Total time for 63992 of 250000 records: 6.49 seconds

• Memory consumption for 63992 of 250000 records: 0.25MB <-> 217.75MB

Page 35: Zf2   how arrays will save your project

ScheduleIntroduction to ZF2

The array

Challenges in development

Solutions offered in ZF2

More options

Page 36: Zf2   how arrays will save your project

Iterators

Page 37: Zf2   how arrays will save your project

Interfaces

Page 38: Zf2   how arrays will save your project

Responsibility Separation

Page 39: Zf2   how arrays will save your project

Modules

Page 40: Zf2   how arrays will save your project

ScheduleIntroduction to ZF2

The array

Challenges in development

Solutions offered in ZF2

More options

Page 41: Zf2   how arrays will save your project

–Michelangelo van Dam

“You dislike arrays because you don’t know them well enough to love them”

Page 42: Zf2   how arrays will save your project

Links

• Array functions http://php.net/manual/en/ref.array.php

• Iterator http://php.net/manual/en/class.iterator.php

• SPL Iterators and DataStructures http://php.net/spl/

Page 43: Zf2   how arrays will save your project

PHPSheatSheetshttp://phpcheatsheets.com/index.php

Page 44: Zf2   how arrays will save your project

http

s://w

ww.

flick

r.com

/pho

tos/

lwr/1

3442

5422

35

Page 45: Zf2   how arrays will save your project

Contact us

in it2PROFESSIONAL PHP SERVICES

Michelangelo van Dam [email protected]

www.in2it.be

PHP Consulting - Training - QA

Page 46: Zf2   how arrays will save your project

Thank youHave a great conference

http

://w

ww.

flick

r.com

/pho

tos/

drew

m/3

1918

7251

5