SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
Another PHP
By Max Gopey
1
Another PHP
•   Chapter 1. Coding standards
•   Chapter 2. OOP
•   Chapter 3. Everything Else
•   Chapter 4.
2
•   PSR
•   Zend
•   PEAR
•   Wordpress
•   Symphony
•   Mediawiki
•   FuelPHP
•   CakePHP
•   CodeIgniter
•   Laravel
•   ...
•   Are
•   you
•   guys
•   MAD?
Chapter 1
Coding standards
3
Chapter 2
OOP
Why do we need objects and classes?
4
Chapter 3
Everything Else
5
Chapter 3
Gitlab Composer
Click me
6
$fetch_refs = function($project) use ($fetch_ref, $repos) {
$datas = array();
try {
foreach (array_merge($repos->branches($project['id']),
$repos->tags($project['id'])) as $ref) {
foreach ($fetch_ref($project, $ref) as $version => $data) {
$datas[$version] = $data;
}
}
} catch (RuntimeException $e) {
// The repo has no commits — skipping it.
}
return $datas;
};
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
7
•   array_column
•   array_map
•   array_search
•   array_reduce
•   array_filter
•   array_walk
•   every / some
Chapter 3
Array Traversing
8
$users = [
['id' => 123, 'first_name' => 'Max', 'last_name' => 'Gopey'],
['id' => 456, 'first_name' => 'Bob', 'last_name' => 'Doe'],
['id' => 789, 'first_name' => 'Alice', 'last_name' => 'Doe'],
];
$lastNames = array_column($users, 'last_name', 'id');
print_r($lastNames);
Array
(
[123] => Max
[456] => Bob
[789] => Alice
)
01.
02.
03.
04.
05.
06.
07.
01.
02.
03.
04.
05.
06.
9
$users = [
['id' => 123, 'first_name' => 'Max', 'last_name' => 'Gopey'],
['id' => 456, 'first_name' => 'Bob', 'last_name' => 'Doe'],
['id' => 789, 'first_name' => 'Alice', 'last_name' => 'Doe'],
];
$fullNames = array_map(function($user) {
return $user['first_name'] . ' ' . $user['last_name'];
}, $users);
print_r($fullNames);
Array
(
[0] => Max Gopey
[1] => Bob Doe
[2] => Alice Doe
)
01.
02.
03.
04.
05.
06.
07.
08.
09.
01.
02.
03.
04.
05.
06.
10
$users = [
['id' => 123, 'first_name' => 'Max', 'last_name' => 'Gopey'],
['id' => 456, 'first_name' => 'Bob', 'last_name' => 'Doe'],
['id' => 789, 'first_name' => 'Alice', 'last_name' => 'Doe'],
];
$index = array_search('Alice Doe', array_map(function($user) {
return $user['first_name'] . ' ' . $user['last_name'];
}, $users));
print_r($index);
2
01.
02.
03.
04.
05.
06.
07.
08.
09.
11
$users = [
['first_name' => 'Max', 'last_name' => 'Gopey', 'company' => 'CGI'],
['first_name' => 'Bob', 'last_name' => 'Doe', 'company' => 'Google'],
['first_name' => 'Alice', 'last_name' => 'Doe', 'company' => 'Google'],
];
 
$byCompany = array_reduce($users, function($result, $user) {
@$result[$user['company']][] = $user['first_name'] . ' ' . $user['last_name'];
return $result;
}, []);
print_r($byCompany);
Array (
[CGI] => Array (
[0] => Max Gopey
)
[Google] => Array (
[0] => Bob Doe
[1] => Alice Doe
)
)
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
01.
02.
03.
04.
05.
06.
07.
08.
09.
12
$users = [
['first_name' => 'Max', 'last_name' => 'Gopey', 'company' => 'CGI'],
['first_name' => 'Bob', 'last_name' => 'Doe', 'company' => 'Google'],
['first_name' => 'Alice', 'last_name' => 'Doe', 'company' => 'Google'],
];
 
$CgiUsers = array_filter($users, function($user) {
return $user['company'] === 'CGI';
});
print_r($CgiUsers);
Array (
[0] => Array (
[first_name] => Max
[last_name] => Gopey
[company] => CGI
)
)
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
01.
02.
03.
04.
05.
06.
07.
13
$users = [
['first_name' => 'Max', 'last_name' => 'Gopey', 'company' => 'CGI'],
['first_name' => 'Bob', 'last_name' => 'Doe', 'company' => 'Google'],
['first_name' => 'Alice', 'last_name' => 'Doe', 'company' => 'Google'],
];
array_walk($users, function(&$user, $index) {
unset($user['last_name'], $user['company']);
$user['first_name'] .= ' ❤';
});
print_r($users);
Array (
[0] => Array (
[first_name] => Max ❤
)
[1] => Array (
[first_name] => Bob ❤
)
[2] => Array(
[first_name] => Alice ❤
)
)
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11. 14
function some($array, $callback) {
foreach ($array as $item) {
if ($callback($item)) {
return true;
}
}
return false;
}
function every($array, $callback) {
return !some($array, function($item) use ($callback) {
return !$callback($item);
});
}
var_dump(every([1, 2, 3], function ($item) {return $item > 0;}));
var_dump(every([1, -2, 3], function ($item) {return $item > 0;}));
bool(true)
bool(false)
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
01.
02.
15
function getBobsAndAlicesWithD($users) {
return array_reduce(
array_filter(
array_map(function($user) {
return $user['last_name'] . ', ' . $user['first_name'];
}, $users),
function($name) {
return stripos($name, 'd') === 0;
}
),
function($result, $value) {
$target = stripos($value, 'bob') !== false ? 'bobs' : 'alices';
$result[$target][] = $value;
return $result;
},
['bobs' => [], 'alices' => []]
);
}
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
16
$users = [
['first_name' => 'Max', 'last_name' => 'Gopey', 'company' => 'CGI'],
['first_name' => 'Bob', 'last_name' => 'Doe', 'company' => 'Google'],
['first_name' => 'Alice', 'last_name' => 'Doe', 'company' => 'Google'],
];
 
print_r(getBobsAndAlicesWithD($users));
Array
(
[bobs] => Array (
[0] => Doe, Bob
)
[alices] => Array (
[0] => Doe, Alice
)
)
01.
02.
03.
04.
05.
06.
07.
17
Chapter 3
Generators
Traversable (Interface)
├── Iterator (Interface)
│ └── Generator (Class)
└── IteratorAggregate (Interface)
18
function garbageGenerator() {
$n = rand(1, 10);
while ($n--) {
yield md5(rand());
}
}
$garbage = garbageGenerator();
foreach ($garbage as $trash) {
echo $trash, PHP_EOL;
}
6e620c902c7088ace3ebf6c96f5dedd5
1340dcc6f3e0e39b4c48f480f5a92d52
c264962d537032be6c3a8a94eda811d4
0bfa2efb3909c105473a4fcaa71b697b
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
19
function readFileLines($path) {
$handle = fopen($path, 'r');
while ($line = fgets($handle)) {
yield $line;
}
fclose($handle);
}
 
$lines = readFileLines(__FILE__);
foreach($lines as $line) {
echo $line;
};
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
20
SymfonyComponentProcessInputStream
Click me
21
function writer(InputStream $stream) {
$stream->write('Message 1');
$stream->write('Message 2');
yield '2 messages written';
$stream->write('Message 3');
$stream->write('Message 4');
yield '2 messages written';
$stream->write('Message 5');
$stream->write('Message 6');
yield '2 messages written';
}
 
function reader(InputStream $stream) {
foreach ($stream as $line) {
if (strlen($line)) {
yield $line;
} else {
$stream->close();
}
}
}
$stream = new InputStream();
$queue[] = writer($stream);
$queue[] = reader($stream);
 
while (true) {
$continue = array_reduce(
$queue,
function($result, Iterator $queueItem) {
if ($valid = $queueItem->valid()) {
echo $queueItem->current(), "n";
$queueItem->next();
}
return $result || $valid;
},
false);
if (!$continue) {
break;
}
}
2 messages written
Message 1
2 messages written
Message 2
2 messages written
Message 3
Message 4
Message 5
Message 6
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
22
Chapter 3
Functional programming
23
function getBobsAndAlicesWithD($users) {
return array_reduce(
array_filter(
array_map(function($user) {
return $user['last_name'] . ', ' . $user['first_name'];
}, $users),
function($name) {
return stripos($name, 'd') === 0;
}
),
function($result, $value) {
$target = stripos($value, 'bob') !== false ? 'bobs' : 'alices';
$result[$target][] = $value;
return $result;
},
['bobs' => [], 'alices' => []]
);
}
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
24
Chapter 3
Non-standard PHP library (NSPL)
Click me
25
$startsWith = function ($string, $substing) {
return stripos($string, $substing) === 0;
};
$contains = function($string, $substing) {
return stripos($string, $substing) !== false;
};
$getFullName = function ($firstName, $lastName) {
return $lastName . ', ' . $firstName;
};
 
$startsWithD = frpartial($startsWith, 'd');
$isBob = frpartial($contains, 'bob');
 
$getFullNameFromUser = function ($user) use ($getFullName) {
return $getFullName($user['first_name'], $user['last_name']);
};
$getStackKey = function($name) use ($isBob) {
return $isBob($name) ? 'bobs' : 'alices';
};
$putToCorrectStack = function($stacks, $value) use ($getStackKey) {
$stacks[$getStackKey($value)][] = $value;
return $stacks;
};
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
26
$getBobsAndAlicesWithD = function ($users)
use ($startsWithD, $getFullNameFromUser, $putToCorrectStack) {
return fpipe(
$users,
fpartial(amap, $getFullNameFromUser),
fpartial(afilter, $startsWithD),
fppartial(areduce, [
0 => $putToCorrectStack,
2 => ['bobs' => [], 'alices' => []]
])
);
};
 
print_r($getBobsAndAlicesWithD($users));
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
27
Chapter 3
Obvious logical operations
28
if (anyOf([1, 3, 5])->is(5)) {
// do something
}
if (anyOf([$name, $surname])->matches('/^w+$/') {
// do something
}
if (allOf([1, 3, 5])->areNot(6)) {
// do something
}
if (either($condition1)->or($condition2)) {
// do something
}
if (neither($x)->nor($y)) {
// do something
}
if (the($x)->isNeither(5)->nor(10)) {
// do something
}
if (the($x)->isGreaterThan(5)->butLessThan(10)) {
// do something
}
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
29
Chapter 3
Obvious regexp
____ _____ ____ _ _ _ ____ _ _ ____
| _  ___ __ _| ____|_ ___ __ | __ ) _ _(_) | __| | ___ _ __| _ | | | | _ 
| |_) / _ / _` | _|  / / '_ | _ | | | | | |/ _` |/ _  '__| |_) | |_| | |_) |
| _ < __/ (_| | |___ > <| |_) | |_) | |_| | | | (_| | __/ | | __/| _ | __/
|_| ____|__, |_____/_/_ .__/|____/ __,_|_|_|__,_|___|_| |_| |_| |_|_|
|___/ |_|
30
$regExp = $builder
->startOfInput()
->exactly(4)->digits()
->then("_")
->exactly(2)->digits()
->then("_")
->min(3)->max(10)->letters()
->then(".")
->anyOf(array("png", "jpg", "gif"))
->endOfInput()
->getRegExp();
 
//true
$regExp->matches("2020_10_hund.jpg");
$regExp->matches("2030_11_katze.png");
$regExp->matches("4000_99_maus.gif");
 
//false
$regExp->matches("123_00_nein.gif");
$regExp->matches("4000_0_nein.pdf");
$regExp->matches("201505_nein.jpg");
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
31
Useful links
Generators and Coroutines
•   Хабр: Coroutines в PHP и работа с неблокирующими функциями
•   Github: Asynchronous coroutines for PHP 7.
•   A Curious Course on Coroutines and Concurrency
•   Symfony/Component/Process/InputStream.php
Functional programming
•   Github: Non-standard PHP library (NSPL) - compact functional programming oriented code
Human-readable regular expressions
•   Github: RegexpBuilderPHP
•   Github: PHPVerbalExpressions
Kittens
•   Youtube: The funniest kitten in the world
32

Weitere ähnliche Inhalte

Was ist angesagt?

Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway ichikaway
 
Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Karsten Dambekalns
 
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonfRafael Dohms
 
Comparing 30 MongoDB operations with Oracle SQL statements
Comparing 30 MongoDB operations with Oracle SQL statementsComparing 30 MongoDB operations with Oracle SQL statements
Comparing 30 MongoDB operations with Oracle SQL statementsLucas Jellema
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistenceHugo Hamon
 
(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and ProfitOlaf Alders
 
Developing applications for performance
Developing applications for performanceDeveloping applications for performance
Developing applications for performanceLeon Fayer
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of LithiumNate Abele
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkG Woo
 
Database performance 101
Database performance 101Database performance 101
Database performance 101Leon Fayer
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of LithiumNate Abele
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Kris Wallsmith
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksNate Abele
 
The Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the QueryThe Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the QueryChris Olbekson
 
PyCon APAC - Django Test Driven Development
PyCon APAC - Django Test Driven DevelopmentPyCon APAC - Django Test Driven Development
PyCon APAC - Django Test Driven DevelopmentTudor Munteanu
 
PHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databasePHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databaseLeon Fayer
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill Chang
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentationguestcf600a
 

Was ist angesagt? (20)

Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
 
Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3
 
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
“Writing code that lasts” … or writing code you won’t hate tomorrow. - PHPKonf
 
Comparing 30 MongoDB operations with Oracle SQL statements
Comparing 30 MongoDB operations with Oracle SQL statementsComparing 30 MongoDB operations with Oracle SQL statements
Comparing 30 MongoDB operations with Oracle SQL statements
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit
 
CakeFest 2013 keynote
CakeFest 2013 keynoteCakeFest 2013 keynote
CakeFest 2013 keynote
 
Developing applications for performance
Developing applications for performanceDeveloping applications for performance
Developing applications for performance
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php framework
 
Database performance 101
Database performance 101Database performance 101
Database performance 101
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of Lithium
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
The Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the QueryThe Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the Query
 
PyCon APAC - Django Test Driven Development
PyCon APAC - Django Test Driven DevelopmentPyCon APAC - Django Test Driven Development
PyCon APAC - Django Test Driven Development
 
PHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databasePHP performance 101: so you need to use a database
PHP performance 101: so you need to use a database
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentation
 

Ähnlich wie How else can you write the code in PHP?

Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)Jeff Eaton
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Seri Moth
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developersStoyan Stefanov
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutesBarang CK
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your CodeAbbas Ali
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7chuvainc
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPVineet Kumar Saini
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Michael Schwern
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with PerlDave Cross
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairMark
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolveXSolve
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix itRafael Dohms
 
Perl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsPerl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsMatt Follett
 
PHP record- with all programs and output
PHP record- with all programs and outputPHP record- with all programs and output
PHP record- with all programs and outputKavithaK23
 

Ähnlich wie How else can you write the code in PHP? (20)

Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutes
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your Code
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
 
D8 Form api
D8 Form apiD8 Form api
D8 Form api
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with Perl
 
Cyclejs introduction
Cyclejs introductionCyclejs introduction
Cyclejs introduction
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love Affair
 
Database api
Database apiDatabase api
Database api
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
 
Perl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsPerl: Hate it for the Right Reasons
Perl: Hate it for the Right Reasons
 
PHP record- with all programs and output
PHP record- with all programs and outputPHP record- with all programs and output
PHP record- with all programs and output
 
Smarty
SmartySmarty
Smarty
 
Presentation1
Presentation1Presentation1
Presentation1
 
Views notwithstanding
Views notwithstandingViews notwithstanding
Views notwithstanding
 

Kürzlich hochgeladen

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 

Kürzlich hochgeladen (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 

How else can you write the code in PHP?

  • 2. Another PHP •   Chapter 1. Coding standards •   Chapter 2. OOP •   Chapter 3. Everything Else •   Chapter 4. 2
  • 3. •   PSR •   Zend •   PEAR •   Wordpress •   Symphony •   Mediawiki •   FuelPHP •   CakePHP •   CodeIgniter •   Laravel •   ... •   Are •   you •   guys •   MAD? Chapter 1 Coding standards 3
  • 4. Chapter 2 OOP Why do we need objects and classes? 4
  • 7. $fetch_refs = function($project) use ($fetch_ref, $repos) { $datas = array(); try { foreach (array_merge($repos->branches($project['id']), $repos->tags($project['id'])) as $ref) { foreach ($fetch_ref($project, $ref) as $version => $data) { $datas[$version] = $data; } } } catch (RuntimeException $e) { // The repo has no commits — skipping it. } return $datas; }; 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 7
  • 8. •   array_column •   array_map •   array_search •   array_reduce •   array_filter •   array_walk •   every / some Chapter 3 Array Traversing 8
  • 9. $users = [ ['id' => 123, 'first_name' => 'Max', 'last_name' => 'Gopey'], ['id' => 456, 'first_name' => 'Bob', 'last_name' => 'Doe'], ['id' => 789, 'first_name' => 'Alice', 'last_name' => 'Doe'], ]; $lastNames = array_column($users, 'last_name', 'id'); print_r($lastNames); Array ( [123] => Max [456] => Bob [789] => Alice ) 01. 02. 03. 04. 05. 06. 07. 01. 02. 03. 04. 05. 06. 9
  • 10. $users = [ ['id' => 123, 'first_name' => 'Max', 'last_name' => 'Gopey'], ['id' => 456, 'first_name' => 'Bob', 'last_name' => 'Doe'], ['id' => 789, 'first_name' => 'Alice', 'last_name' => 'Doe'], ]; $fullNames = array_map(function($user) { return $user['first_name'] . ' ' . $user['last_name']; }, $users); print_r($fullNames); Array ( [0] => Max Gopey [1] => Bob Doe [2] => Alice Doe ) 01. 02. 03. 04. 05. 06. 07. 08. 09. 01. 02. 03. 04. 05. 06. 10
  • 11. $users = [ ['id' => 123, 'first_name' => 'Max', 'last_name' => 'Gopey'], ['id' => 456, 'first_name' => 'Bob', 'last_name' => 'Doe'], ['id' => 789, 'first_name' => 'Alice', 'last_name' => 'Doe'], ]; $index = array_search('Alice Doe', array_map(function($user) { return $user['first_name'] . ' ' . $user['last_name']; }, $users)); print_r($index); 2 01. 02. 03. 04. 05. 06. 07. 08. 09. 11
  • 12. $users = [ ['first_name' => 'Max', 'last_name' => 'Gopey', 'company' => 'CGI'], ['first_name' => 'Bob', 'last_name' => 'Doe', 'company' => 'Google'], ['first_name' => 'Alice', 'last_name' => 'Doe', 'company' => 'Google'], ];   $byCompany = array_reduce($users, function($result, $user) { @$result[$user['company']][] = $user['first_name'] . ' ' . $user['last_name']; return $result; }, []); print_r($byCompany); Array ( [CGI] => Array ( [0] => Max Gopey ) [Google] => Array ( [0] => Bob Doe [1] => Alice Doe ) ) 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 01. 02. 03. 04. 05. 06. 07. 08. 09. 12
  • 13. $users = [ ['first_name' => 'Max', 'last_name' => 'Gopey', 'company' => 'CGI'], ['first_name' => 'Bob', 'last_name' => 'Doe', 'company' => 'Google'], ['first_name' => 'Alice', 'last_name' => 'Doe', 'company' => 'Google'], ];   $CgiUsers = array_filter($users, function($user) { return $user['company'] === 'CGI'; }); print_r($CgiUsers); Array ( [0] => Array ( [first_name] => Max [last_name] => Gopey [company] => CGI ) ) 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 01. 02. 03. 04. 05. 06. 07. 13
  • 14. $users = [ ['first_name' => 'Max', 'last_name' => 'Gopey', 'company' => 'CGI'], ['first_name' => 'Bob', 'last_name' => 'Doe', 'company' => 'Google'], ['first_name' => 'Alice', 'last_name' => 'Doe', 'company' => 'Google'], ]; array_walk($users, function(&$user, $index) { unset($user['last_name'], $user['company']); $user['first_name'] .= ' ❤'; }); print_r($users); Array ( [0] => Array ( [first_name] => Max ❤ ) [1] => Array ( [first_name] => Bob ❤ ) [2] => Array( [first_name] => Alice ❤ ) ) 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 14
  • 15. function some($array, $callback) { foreach ($array as $item) { if ($callback($item)) { return true; } } return false; } function every($array, $callback) { return !some($array, function($item) use ($callback) { return !$callback($item); }); } var_dump(every([1, 2, 3], function ($item) {return $item > 0;})); var_dump(every([1, -2, 3], function ($item) {return $item > 0;})); bool(true) bool(false) 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 01. 02. 15
  • 16. function getBobsAndAlicesWithD($users) { return array_reduce( array_filter( array_map(function($user) { return $user['last_name'] . ', ' . $user['first_name']; }, $users), function($name) { return stripos($name, 'd') === 0; } ), function($result, $value) { $target = stripos($value, 'bob') !== false ? 'bobs' : 'alices'; $result[$target][] = $value; return $result; }, ['bobs' => [], 'alices' => []] ); } 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 16
  • 17. $users = [ ['first_name' => 'Max', 'last_name' => 'Gopey', 'company' => 'CGI'], ['first_name' => 'Bob', 'last_name' => 'Doe', 'company' => 'Google'], ['first_name' => 'Alice', 'last_name' => 'Doe', 'company' => 'Google'], ];   print_r(getBobsAndAlicesWithD($users)); Array ( [bobs] => Array ( [0] => Doe, Bob ) [alices] => Array ( [0] => Doe, Alice ) ) 01. 02. 03. 04. 05. 06. 07. 17
  • 18. Chapter 3 Generators Traversable (Interface) ├── Iterator (Interface) │ └── Generator (Class) └── IteratorAggregate (Interface) 18
  • 19. function garbageGenerator() { $n = rand(1, 10); while ($n--) { yield md5(rand()); } } $garbage = garbageGenerator(); foreach ($garbage as $trash) { echo $trash, PHP_EOL; } 6e620c902c7088ace3ebf6c96f5dedd5 1340dcc6f3e0e39b4c48f480f5a92d52 c264962d537032be6c3a8a94eda811d4 0bfa2efb3909c105473a4fcaa71b697b 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 19
  • 20. function readFileLines($path) { $handle = fopen($path, 'r'); while ($line = fgets($handle)) { yield $line; } fclose($handle); }   $lines = readFileLines(__FILE__); foreach($lines as $line) { echo $line; }; 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 20
  • 22. function writer(InputStream $stream) { $stream->write('Message 1'); $stream->write('Message 2'); yield '2 messages written'; $stream->write('Message 3'); $stream->write('Message 4'); yield '2 messages written'; $stream->write('Message 5'); $stream->write('Message 6'); yield '2 messages written'; }   function reader(InputStream $stream) { foreach ($stream as $line) { if (strlen($line)) { yield $line; } else { $stream->close(); } } } $stream = new InputStream(); $queue[] = writer($stream); $queue[] = reader($stream);   while (true) { $continue = array_reduce( $queue, function($result, Iterator $queueItem) { if ($valid = $queueItem->valid()) { echo $queueItem->current(), "n"; $queueItem->next(); } return $result || $valid; }, false); if (!$continue) { break; } } 2 messages written Message 1 2 messages written Message 2 2 messages written Message 3 Message 4 Message 5 Message 6 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 22
  • 24. function getBobsAndAlicesWithD($users) { return array_reduce( array_filter( array_map(function($user) { return $user['last_name'] . ', ' . $user['first_name']; }, $users), function($name) { return stripos($name, 'd') === 0; } ), function($result, $value) { $target = stripos($value, 'bob') !== false ? 'bobs' : 'alices'; $result[$target][] = $value; return $result; }, ['bobs' => [], 'alices' => []] ); } 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 24
  • 25. Chapter 3 Non-standard PHP library (NSPL) Click me 25
  • 26. $startsWith = function ($string, $substing) { return stripos($string, $substing) === 0; }; $contains = function($string, $substing) { return stripos($string, $substing) !== false; }; $getFullName = function ($firstName, $lastName) { return $lastName . ', ' . $firstName; };   $startsWithD = frpartial($startsWith, 'd'); $isBob = frpartial($contains, 'bob');   $getFullNameFromUser = function ($user) use ($getFullName) { return $getFullName($user['first_name'], $user['last_name']); }; $getStackKey = function($name) use ($isBob) { return $isBob($name) ? 'bobs' : 'alices'; }; $putToCorrectStack = function($stacks, $value) use ($getStackKey) { $stacks[$getStackKey($value)][] = $value; return $stacks; }; 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 26
  • 27. $getBobsAndAlicesWithD = function ($users) use ($startsWithD, $getFullNameFromUser, $putToCorrectStack) { return fpipe( $users, fpartial(amap, $getFullNameFromUser), fpartial(afilter, $startsWithD), fppartial(areduce, [ 0 => $putToCorrectStack, 2 => ['bobs' => [], 'alices' => []] ]) ); };   print_r($getBobsAndAlicesWithD($users)); 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 27
  • 28. Chapter 3 Obvious logical operations 28
  • 29. if (anyOf([1, 3, 5])->is(5)) { // do something } if (anyOf([$name, $surname])->matches('/^w+$/') { // do something } if (allOf([1, 3, 5])->areNot(6)) { // do something } if (either($condition1)->or($condition2)) { // do something } if (neither($x)->nor($y)) { // do something } if (the($x)->isNeither(5)->nor(10)) { // do something } if (the($x)->isGreaterThan(5)->butLessThan(10)) { // do something } 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 29
  • 30. Chapter 3 Obvious regexp ____ _____ ____ _ _ _ ____ _ _ ____ | _ ___ __ _| ____|_ ___ __ | __ ) _ _(_) | __| | ___ _ __| _ | | | | _ | |_) / _ / _` | _| / / '_ | _ | | | | | |/ _` |/ _ '__| |_) | |_| | |_) | | _ < __/ (_| | |___ > <| |_) | |_) | |_| | | | (_| | __/ | | __/| _ | __/ |_| ____|__, |_____/_/_ .__/|____/ __,_|_|_|__,_|___|_| |_| |_| |_|_| |___/ |_| 30
  • 31. $regExp = $builder ->startOfInput() ->exactly(4)->digits() ->then("_") ->exactly(2)->digits() ->then("_") ->min(3)->max(10)->letters() ->then(".") ->anyOf(array("png", "jpg", "gif")) ->endOfInput() ->getRegExp();   //true $regExp->matches("2020_10_hund.jpg"); $regExp->matches("2030_11_katze.png"); $regExp->matches("4000_99_maus.gif");   //false $regExp->matches("123_00_nein.gif"); $regExp->matches("4000_0_nein.pdf"); $regExp->matches("201505_nein.jpg"); 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 31
  • 32. Useful links Generators and Coroutines •   Хабр: Coroutines в PHP и работа с неблокирующими функциями •   Github: Asynchronous coroutines for PHP 7. •   A Curious Course on Coroutines and Concurrency •   Symfony/Component/Process/InputStream.php Functional programming •   Github: Non-standard PHP library (NSPL) - compact functional programming oriented code Human-readable regular expressions •   Github: RegexpBuilderPHP •   Github: PHPVerbalExpressions Kittens •   Youtube: The funniest kitten in the world 32