ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
THE CALLBACK HELL
a sneak peak!
Getting out of
¡°The situation where callbacks are
nested within other callbacks several
levels deep, potentially making it
di?cult to understand and maintain
the code.
-Wiktionary
USE OF CALLBACKS
Why just wait for
Disk reading/writing
Network reading/writing
Communication with DB
Sending mail
When we have other things to
do
USE OF CALLBACKS
an example
WHY WE NEED CALLBACKS?
Typically
PHP JS
NOW PHP CAN DO ASYNC
? Several built-in functions
? Several libraries (using the
built-in functions)
? Facebook Hack
NOW PHP CAN DO ASYNC
? Several built-in functions
? Using Curl as async
system
? Several libraries (using the
built-in functions)
? ReactPHP
? Facebook Hack
CALLBACK
$web3 = new Web3('http://localhost:8545');
$eth = $web3->eth;
$eth->accounts(function ($err, $accounts) use ($eth) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
$eth->getBalance($accounts[0], function ($err, $balance) use ($eth, $accounts) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
echo $accounts[0] . ' Balance: ' . $balance . PHP_EOL;
$eth->getBalance($accounts[1], function ($err, $balance2) use ($eth, $accounts, $balance) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
echo $accounts[1] . ' Balance: ' . $balance2 . PHP_EOL;
$total = $balance->add($balance2);
echo 'Total Balance: ' . $total . PHP_EOL;
});
});
});
<?php
require __DIR__ . '/../vendor/autoload.php';
use Web3Web3;
try {
$web3 = new Web3('http://localhost:8545');
$eth = $web3->eth;
$accounts = $eth->accounts();
$balance = $eth->getBalance($accounts[0]);
echo $accounts[0] . ' Balance: ' . $balance . PHP_EOL;
$balance2 = $eth->getBalance($accounts[1]);
echo $accounts[1] . ' Balance: ' . $balance2 . PHP_EOL;
$total = $balance->add($balance2);
echo 'Total Balance: ' . $total . PHP_EOL;
} catch (Throwable $err) {
echo 'Error: ' . $err->getMessage();
}
/*
0x262bc77dfc5748620641325b3617fc07b20f0b92 Balance: 100000000000000000000
0x2d8785375886cf900126cf3082aad6f47427a1d7 Balance: 100000000000000000000
Total Balance: 200000000000000000000
*/
SYNC
SYNCuse Web3Web3;
function balance()
{
try {
$web3 = new Web3('http://localhost:8545');
$eth = $web3->eth;
$accounts = $eth->accounts();
$balance = $eth->getBalance($accounts[0]);
echo $accounts[0] . ' Balance: ' . $balance . PHP_EOL;
$balance2 = $eth->getBalance($accounts[1]);
echo $accounts[1] . ' Balance: ' . $balance2 . PHP_EOL;
$total = $balance->add($balance2);
echo 'Total Balance: ' . $total . PHP_EOL;
return $total;
} catch (Throwable $err) {
echo 'Error: ' . $err->getMessage();
}
}
balance();
/*
0x262bc77dfc5748620641325b3617fc07b20f0b92 Balance: 100000000000000000000
0x2d8785375886cf900126cf3082aad6f47427a1d7 Balance: 100000000000000000000
Total Balance: 200000000000000000000
*/
use Web3Web3;
function balance(): Generator
{
try {
$web3 = new Web3('http://localhost:8545');
$eth = $web3->eth;
$accounts = yield [$eth, 'accounts'];
$balance = yield [$eth, 'getBalance', $accounts[0]];
echo $accounts[0] . ' Balance: ' . $balance . PHP_EOL;
$balance2 = yield [$eth, 'getBalance', $accounts[1]];
echo $accounts[1] . ' Balance: ' . $balance2 . PHP_EOL;
$total = $balance->add($balance2);
echo 'Total Balance: ' . $total . PHP_EOL;
return $total;
} catch (Throwable $err) {
echo 'Error: ' . $err->getMessage();
}
}
async(balance());
/*
0x262bc77dfc5748620641325b3617fc07b20f0b92 Balance: 100000000000000000000
0x2d8785375886cf900126cf3082aad6f47427a1d7 Balance: 100000000000000000000
Total Balance: 200000000000000000000
*/
ASYNC
function balance()
{
try {
$web3 = new Web3('http://localhost:8545');
$eth = $web3->eth;
$accounts = $eth->accounts();
$total = new BigInteger();
foreach ($accounts as $account) {
$balance = $eth->getBalance($account);
echo $accounts . ' Balance: ' . $balance . PHP_EOL;
$total = $total->add($balance);
}
echo 'Total Balance: ' . $total . PHP_EOL;
return $total;
} catch (Throwable $err) {
echo 'Error: ' . $err->getMessage();
}
}
balance();
SYNC
function balance(): Generator
{
try {
$web3 = new Web3('http://localhost:8545');
$eth = $web3->eth;
$accounts = yield [$eth, 'accounts'];
$total = new BigInteger();
foreach ($accounts as $account) {
$balance = yield [$eth, 'getBalance', $account];
echo $account . ' Balance: ' . $balance . PHP_EOL;
$total = $total->add($balance);
}
echo 'Total Balance: ' . $total . PHP_EOL;
return $total;
} catch (Throwable $err) {
echo 'Error: ' . $err->getMessage();
}
}
async(balance());
ASYNC

More Related Content

What's hot (6)

isa architecture
isa architectureisa architecture
isa architecture
AJAL A J
?
Backtracking Algorithm.ppt
Backtracking Algorithm.pptBacktracking Algorithm.ppt
Backtracking Algorithm.ppt
SalmIbrahimIlyas
?
R¨¦tro-escalade sur la notion de cycle de vieR¨¦tro-escalade sur la notion de cycle de vie
R¨¦tro-escalade sur la notion de cycle de vie
Forum des archivistes de l'arc l¨¦manique
?
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
Praveen M Jigajinni
?
why to do BCA course?
why to do BCA course?why to do BCA course?
why to do BCA course?
L.POONGOTHAI SHALINI
?
Theory of programming
Theory of programmingTheory of programming
Theory of programming
tcc_joemarie
?
isa architecture
isa architectureisa architecture
isa architecture
AJAL A J
?
R¨¦tro-escalade sur la notion de cycle de vieR¨¦tro-escalade sur la notion de cycle de vie
R¨¦tro-escalade sur la notion de cycle de vie
Forum des archivistes de l'arc l¨¦manique
?
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
Praveen M Jigajinni
?
Theory of programming
Theory of programmingTheory of programming
Theory of programming
tcc_joemarie
?

Similar to Getting out of Callback Hell in PHP (20)

Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2
markstory
?
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
Elena Kolevska
?
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
Ian Barber
?
Intro to php
Intro to phpIntro to php
Intro to php
Sp Singh
?
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
Kacper Gunia
?
Swoole Overview
Swoole OverviewSwoole Overview
Swoole Overview
Manuel Baldassarri
?
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
Bradley Holt
?
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
Elizabeth Smith
?
Symfony internals [english]
Symfony internals [english]Symfony internals [english]
Symfony internals [english]
Raul Fraile
?
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
elliando dias
?
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
?
Fatc
FatcFatc
Fatc
Wade Arnold
?
¹ó²¹³¦±ð²ú´Ç´Ç°ìµÄ»º´æϵͳ
¹ó²¹³¦±ð²ú´Ç´Ç°ìµÄ»º´æϵͳ¹ó²¹³¦±ð²ú´Ç´Ç°ìµÄ»º´æϵͳ
¹ó²¹³¦±ð²ú´Ç´Ç°ìµÄ»º´æϵͳ
yiditushe
?
Php hacku
Php hackuPhp hacku
Php hacku
Tom Praison Praison
?
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
Marcello Duarte
?
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
?
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
Mohammad Imam Hossain
?
Zend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample QuestionsZend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample Questions
Jagat Kothari
?
DBD::SQLite
DBD::SQLiteDBD::SQLite
DBD::SQLite
charsbar
?
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
guoqing75
?
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2
markstory
?
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
Elena Kolevska
?
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
Ian Barber
?
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
Kacper Gunia
?
Symfony internals [english]
Symfony internals [english]Symfony internals [english]
Symfony internals [english]
Raul Fraile
?
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
elliando dias
?
¹ó²¹³¦±ð²ú´Ç´Ç°ìµÄ»º´æϵͳ
¹ó²¹³¦±ð²ú´Ç´Ç°ìµÄ»º´æϵͳ¹ó²¹³¦±ð²ú´Ç´Ç°ìµÄ»º´æϵͳ
¹ó²¹³¦±ð²ú´Ç´Ç°ìµÄ»º´æϵͳ
yiditushe
?
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
?
Zend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample QuestionsZend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample Questions
Jagat Kothari
?
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
guoqing75
?

More from Arul Kumaran (8)

Accelerating Xamarin Development
Accelerating Xamarin DevelopmentAccelerating Xamarin Development
Accelerating Xamarin Development
Arul Kumaran
?
iOS Native Development with Xamarin
iOS Native Development with XamariniOS Native Development with Xamarin
iOS Native Development with Xamarin
Arul Kumaran
?
Testing and Documenting Pragmatic / RESTful Web API
Testing and Documenting Pragmatic / RESTful Web APITesting and Documenting Pragmatic / RESTful Web API
Testing and Documenting Pragmatic / RESTful Web API
Arul Kumaran
?
Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0
Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0
Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0
Arul Kumaran
?
Less Verbose ActionScript 3.0 - Write less and do more!
Less Verbose ActionScript 3.0 - Write less and do more!Less Verbose ActionScript 3.0 - Write less and do more!
Less Verbose ActionScript 3.0 - Write less and do more!
Arul Kumaran
?
Using Titanium for multi-platform development including iPhone and Android
Using Titanium for multi-platform development including iPhone and AndroidUsing Titanium for multi-platform development including iPhone and Android
Using Titanium for multi-platform development including iPhone and Android
Arul Kumaran
?
UI Interactions Testing with FlexMonkey
UI Interactions Testing with FlexMonkeyUI Interactions Testing with FlexMonkey
UI Interactions Testing with FlexMonkey
Arul Kumaran
?
Flex Production Tips & Techniques
Flex Production Tips & TechniquesFlex Production Tips & Techniques
Flex Production Tips & Techniques
Arul Kumaran
?
Accelerating Xamarin Development
Accelerating Xamarin DevelopmentAccelerating Xamarin Development
Accelerating Xamarin Development
Arul Kumaran
?
iOS Native Development with Xamarin
iOS Native Development with XamariniOS Native Development with Xamarin
iOS Native Development with Xamarin
Arul Kumaran
?
Testing and Documenting Pragmatic / RESTful Web API
Testing and Documenting Pragmatic / RESTful Web APITesting and Documenting Pragmatic / RESTful Web API
Testing and Documenting Pragmatic / RESTful Web API
Arul Kumaran
?
Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0
Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0
Taking Care of The REST - Creating your own RESTful API Server using Restler 2.0
Arul Kumaran
?
Less Verbose ActionScript 3.0 - Write less and do more!
Less Verbose ActionScript 3.0 - Write less and do more!Less Verbose ActionScript 3.0 - Write less and do more!
Less Verbose ActionScript 3.0 - Write less and do more!
Arul Kumaran
?
Using Titanium for multi-platform development including iPhone and Android
Using Titanium for multi-platform development including iPhone and AndroidUsing Titanium for multi-platform development including iPhone and Android
Using Titanium for multi-platform development including iPhone and Android
Arul Kumaran
?
UI Interactions Testing with FlexMonkey
UI Interactions Testing with FlexMonkeyUI Interactions Testing with FlexMonkey
UI Interactions Testing with FlexMonkey
Arul Kumaran
?
Flex Production Tips & Techniques
Flex Production Tips & TechniquesFlex Production Tips & Techniques
Flex Production Tips & Techniques
Arul Kumaran
?

Recently uploaded (20)

What is Blockchain and How Can Blockchain Consulting Help Businesses.pdf
What is Blockchain and How Can Blockchain Consulting Help Businesses.pdfWhat is Blockchain and How Can Blockchain Consulting Help Businesses.pdf
What is Blockchain and How Can Blockchain Consulting Help Businesses.pdf
Yodaplus Technologies Private Limited
?
Kickstart Your QA: An Introduction to Automated Regression Testing Tools
Kickstart Your QA: An Introduction to Automated Regression Testing ToolsKickstart Your QA: An Introduction to Automated Regression Testing Tools
Kickstart Your QA: An Introduction to Automated Regression Testing Tools
Shubham Joshi
?
The Constructor's Digital Transformation Playbook: Reducing Risk With Technology
The Constructor's Digital Transformation Playbook: Reducing Risk With TechnologyThe Constructor's Digital Transformation Playbook: Reducing Risk With Technology
The Constructor's Digital Transformation Playbook: Reducing Risk With Technology
Aggregage
?
Understanding & Utilizing SharePoint Advanced Management
Understanding & Utilizing SharePoint Advanced ManagementUnderstanding & Utilizing SharePoint Advanced Management
Understanding & Utilizing SharePoint Advanced Management
Drew Madelung
?
Not a Kubernetes fan? The state of PaaS in 2025
Not a Kubernetes fan? The state of PaaS in 2025Not a Kubernetes fan? The state of PaaS in 2025
Not a Kubernetes fan? The state of PaaS in 2025
Anthony Dahanne
?
Deno ...................................
Deno ...................................Deno ...................................
Deno ...................................
Robert MacLean
?
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
?
2025-02-24 - AWS meetup - Zilliz presentation.pdf
2025-02-24 - AWS meetup - Zilliz presentation.pdf2025-02-24 - AWS meetup - Zilliz presentation.pdf
2025-02-24 - AWS meetup - Zilliz presentation.pdf
Ivan Tang
?
5 Must-Use AI Tools to Supercharge Your Productivity
5 Must-Use AI Tools to Supercharge Your Productivity5 Must-Use AI Tools to Supercharge Your Productivity
5 Must-Use AI Tools to Supercharge Your Productivity
cryptouniversityoffi
?
Caching for Performance Masterclass: The In-Memory Datastore
Caching for Performance Masterclass: The In-Memory DatastoreCaching for Performance Masterclass: The In-Memory Datastore
Caching for Performance Masterclass: The In-Memory Datastore
ScyllaDB
?
Combining Lexical and Semantic Search with Milvus 2.5
Combining Lexical and Semantic Search with Milvus 2.5Combining Lexical and Semantic Search with Milvus 2.5
Combining Lexical and Semantic Search with Milvus 2.5
Zilliz
?
Getting Started with AWS - Enterprise Landing Zone for Terraform Learning & D...
Getting Started with AWS - Enterprise Landing Zone for Terraform Learning & D...Getting Started with AWS - Enterprise Landing Zone for Terraform Learning & D...
Getting Started with AWS - Enterprise Landing Zone for Terraform Learning & D...
Chris Wahl
?
Caching for Performance Masterclass: Caching at Scale
Caching for Performance Masterclass: Caching at ScaleCaching for Performance Masterclass: Caching at Scale
Caching for Performance Masterclass: Caching at Scale
ScyllaDB
?
Teaching Prompting and Prompt Sharing to End Users.pptx
Teaching Prompting and Prompt Sharing to End Users.pptxTeaching Prompting and Prompt Sharing to End Users.pptx
Teaching Prompting and Prompt Sharing to End Users.pptx
Michael Blumenthal (Microsoft MVP)
?
Data-Driven Public Safety: Reliable Data When Every Second Counts
Data-Driven Public Safety: Reliable Data When Every Second CountsData-Driven Public Safety: Reliable Data When Every Second Counts
Data-Driven Public Safety: Reliable Data When Every Second Counts
Safe Software
?
16 KALALU????????????????????? APARAMAHASAHASRA SIMHAMAHANKALKIADIPARASAKTIBH...
16 KALALU????????????????????? APARAMAHASAHASRA SIMHAMAHANKALKIADIPARASAKTIBH...16 KALALU????????????????????? APARAMAHASAHASRA SIMHAMAHANKALKIADIPARASAKTIBH...
16 KALALU????????????????????? APARAMAHASAHASRA SIMHAMAHANKALKIADIPARASAKTIBH...
IT Industry
?
SECURE BLOCKCHAIN FOR ADMISSION PROCESSING IN EDUCATIONAL INSTITUTIONS.pdf
SECURE BLOCKCHAIN FOR ADMISSION PROCESSING IN EDUCATIONAL INSTITUTIONS.pdfSECURE BLOCKCHAIN FOR ADMISSION PROCESSING IN EDUCATIONAL INSTITUTIONS.pdf
SECURE BLOCKCHAIN FOR ADMISSION PROCESSING IN EDUCATIONAL INSTITUTIONS.pdf
spub1985
?
Webinar: LF Energy GEISA: Addressing edge interoperability at the meter
Webinar: LF Energy GEISA: Addressing edge interoperability at the meterWebinar: LF Energy GEISA: Addressing edge interoperability at the meter
Webinar: LF Energy GEISA: Addressing edge interoperability at the meter
DanBrown980551
?
GDG Cloud Southlake #40: Brandon Stokes: How to Build a Great Product
GDG Cloud Southlake #40: Brandon Stokes: How to Build a Great ProductGDG Cloud Southlake #40: Brandon Stokes: How to Build a Great Product
GDG Cloud Southlake #40: Brandon Stokes: How to Build a Great Product
James Anderson
?
Revolutionizing Field Service: How LLMs Are Powering Smarter Knowledge Access...
Revolutionizing Field Service: How LLMs Are Powering Smarter Knowledge Access...Revolutionizing Field Service: How LLMs Are Powering Smarter Knowledge Access...
Revolutionizing Field Service: How LLMs Are Powering Smarter Knowledge Access...
Earley Information Science
?
Kickstart Your QA: An Introduction to Automated Regression Testing Tools
Kickstart Your QA: An Introduction to Automated Regression Testing ToolsKickstart Your QA: An Introduction to Automated Regression Testing Tools
Kickstart Your QA: An Introduction to Automated Regression Testing Tools
Shubham Joshi
?
The Constructor's Digital Transformation Playbook: Reducing Risk With Technology
The Constructor's Digital Transformation Playbook: Reducing Risk With TechnologyThe Constructor's Digital Transformation Playbook: Reducing Risk With Technology
The Constructor's Digital Transformation Playbook: Reducing Risk With Technology
Aggregage
?
Understanding & Utilizing SharePoint Advanced Management
Understanding & Utilizing SharePoint Advanced ManagementUnderstanding & Utilizing SharePoint Advanced Management
Understanding & Utilizing SharePoint Advanced Management
Drew Madelung
?
Not a Kubernetes fan? The state of PaaS in 2025
Not a Kubernetes fan? The state of PaaS in 2025Not a Kubernetes fan? The state of PaaS in 2025
Not a Kubernetes fan? The state of PaaS in 2025
Anthony Dahanne
?
Deno ...................................
Deno ...................................Deno ...................................
Deno ...................................
Robert MacLean
?
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
?
2025-02-24 - AWS meetup - Zilliz presentation.pdf
2025-02-24 - AWS meetup - Zilliz presentation.pdf2025-02-24 - AWS meetup - Zilliz presentation.pdf
2025-02-24 - AWS meetup - Zilliz presentation.pdf
Ivan Tang
?
5 Must-Use AI Tools to Supercharge Your Productivity
5 Must-Use AI Tools to Supercharge Your Productivity5 Must-Use AI Tools to Supercharge Your Productivity
5 Must-Use AI Tools to Supercharge Your Productivity
cryptouniversityoffi
?
Caching for Performance Masterclass: The In-Memory Datastore
Caching for Performance Masterclass: The In-Memory DatastoreCaching for Performance Masterclass: The In-Memory Datastore
Caching for Performance Masterclass: The In-Memory Datastore
ScyllaDB
?
Combining Lexical and Semantic Search with Milvus 2.5
Combining Lexical and Semantic Search with Milvus 2.5Combining Lexical and Semantic Search with Milvus 2.5
Combining Lexical and Semantic Search with Milvus 2.5
Zilliz
?
Getting Started with AWS - Enterprise Landing Zone for Terraform Learning & D...
Getting Started with AWS - Enterprise Landing Zone for Terraform Learning & D...Getting Started with AWS - Enterprise Landing Zone for Terraform Learning & D...
Getting Started with AWS - Enterprise Landing Zone for Terraform Learning & D...
Chris Wahl
?
Caching for Performance Masterclass: Caching at Scale
Caching for Performance Masterclass: Caching at ScaleCaching for Performance Masterclass: Caching at Scale
Caching for Performance Masterclass: Caching at Scale
ScyllaDB
?
Data-Driven Public Safety: Reliable Data When Every Second Counts
Data-Driven Public Safety: Reliable Data When Every Second CountsData-Driven Public Safety: Reliable Data When Every Second Counts
Data-Driven Public Safety: Reliable Data When Every Second Counts
Safe Software
?
16 KALALU????????????????????? APARAMAHASAHASRA SIMHAMAHANKALKIADIPARASAKTIBH...
16 KALALU????????????????????? APARAMAHASAHASRA SIMHAMAHANKALKIADIPARASAKTIBH...16 KALALU????????????????????? APARAMAHASAHASRA SIMHAMAHANKALKIADIPARASAKTIBH...
16 KALALU????????????????????? APARAMAHASAHASRA SIMHAMAHANKALKIADIPARASAKTIBH...
IT Industry
?
SECURE BLOCKCHAIN FOR ADMISSION PROCESSING IN EDUCATIONAL INSTITUTIONS.pdf
SECURE BLOCKCHAIN FOR ADMISSION PROCESSING IN EDUCATIONAL INSTITUTIONS.pdfSECURE BLOCKCHAIN FOR ADMISSION PROCESSING IN EDUCATIONAL INSTITUTIONS.pdf
SECURE BLOCKCHAIN FOR ADMISSION PROCESSING IN EDUCATIONAL INSTITUTIONS.pdf
spub1985
?
Webinar: LF Energy GEISA: Addressing edge interoperability at the meter
Webinar: LF Energy GEISA: Addressing edge interoperability at the meterWebinar: LF Energy GEISA: Addressing edge interoperability at the meter
Webinar: LF Energy GEISA: Addressing edge interoperability at the meter
DanBrown980551
?
GDG Cloud Southlake #40: Brandon Stokes: How to Build a Great Product
GDG Cloud Southlake #40: Brandon Stokes: How to Build a Great ProductGDG Cloud Southlake #40: Brandon Stokes: How to Build a Great Product
GDG Cloud Southlake #40: Brandon Stokes: How to Build a Great Product
James Anderson
?
Revolutionizing Field Service: How LLMs Are Powering Smarter Knowledge Access...
Revolutionizing Field Service: How LLMs Are Powering Smarter Knowledge Access...Revolutionizing Field Service: How LLMs Are Powering Smarter Knowledge Access...
Revolutionizing Field Service: How LLMs Are Powering Smarter Knowledge Access...
Earley Information Science
?

Getting out of Callback Hell in PHP

  • 1. THE CALLBACK HELL a sneak peak! Getting out of
  • 2. ¡°The situation where callbacks are nested within other callbacks several levels deep, potentially making it di?cult to understand and maintain the code. -Wiktionary
  • 3. USE OF CALLBACKS Why just wait for Disk reading/writing Network reading/writing Communication with DB Sending mail When we have other things to do
  • 5. WHY WE NEED CALLBACKS? Typically PHP JS
  • 6. NOW PHP CAN DO ASYNC ? Several built-in functions ? Several libraries (using the built-in functions) ? Facebook Hack
  • 7. NOW PHP CAN DO ASYNC ? Several built-in functions ? Using Curl as async system ? Several libraries (using the built-in functions) ? ReactPHP ? Facebook Hack
  • 8. CALLBACK $web3 = new Web3('http://localhost:8545'); $eth = $web3->eth; $eth->accounts(function ($err, $accounts) use ($eth) { if ($err !== null) { echo 'Error: ' . $err->getMessage(); return; } $eth->getBalance($accounts[0], function ($err, $balance) use ($eth, $accounts) { if ($err !== null) { echo 'Error: ' . $err->getMessage(); return; } echo $accounts[0] . ' Balance: ' . $balance . PHP_EOL; $eth->getBalance($accounts[1], function ($err, $balance2) use ($eth, $accounts, $balance) { if ($err !== null) { echo 'Error: ' . $err->getMessage(); return; } echo $accounts[1] . ' Balance: ' . $balance2 . PHP_EOL; $total = $balance->add($balance2); echo 'Total Balance: ' . $total . PHP_EOL; }); }); });
  • 9. <?php require __DIR__ . '/../vendor/autoload.php'; use Web3Web3; try { $web3 = new Web3('http://localhost:8545'); $eth = $web3->eth; $accounts = $eth->accounts(); $balance = $eth->getBalance($accounts[0]); echo $accounts[0] . ' Balance: ' . $balance . PHP_EOL; $balance2 = $eth->getBalance($accounts[1]); echo $accounts[1] . ' Balance: ' . $balance2 . PHP_EOL; $total = $balance->add($balance2); echo 'Total Balance: ' . $total . PHP_EOL; } catch (Throwable $err) { echo 'Error: ' . $err->getMessage(); } /* 0x262bc77dfc5748620641325b3617fc07b20f0b92 Balance: 100000000000000000000 0x2d8785375886cf900126cf3082aad6f47427a1d7 Balance: 100000000000000000000 Total Balance: 200000000000000000000 */ SYNC
  • 10. SYNCuse Web3Web3; function balance() { try { $web3 = new Web3('http://localhost:8545'); $eth = $web3->eth; $accounts = $eth->accounts(); $balance = $eth->getBalance($accounts[0]); echo $accounts[0] . ' Balance: ' . $balance . PHP_EOL; $balance2 = $eth->getBalance($accounts[1]); echo $accounts[1] . ' Balance: ' . $balance2 . PHP_EOL; $total = $balance->add($balance2); echo 'Total Balance: ' . $total . PHP_EOL; return $total; } catch (Throwable $err) { echo 'Error: ' . $err->getMessage(); } } balance(); /* 0x262bc77dfc5748620641325b3617fc07b20f0b92 Balance: 100000000000000000000 0x2d8785375886cf900126cf3082aad6f47427a1d7 Balance: 100000000000000000000 Total Balance: 200000000000000000000 */
  • 11. use Web3Web3; function balance(): Generator { try { $web3 = new Web3('http://localhost:8545'); $eth = $web3->eth; $accounts = yield [$eth, 'accounts']; $balance = yield [$eth, 'getBalance', $accounts[0]]; echo $accounts[0] . ' Balance: ' . $balance . PHP_EOL; $balance2 = yield [$eth, 'getBalance', $accounts[1]]; echo $accounts[1] . ' Balance: ' . $balance2 . PHP_EOL; $total = $balance->add($balance2); echo 'Total Balance: ' . $total . PHP_EOL; return $total; } catch (Throwable $err) { echo 'Error: ' . $err->getMessage(); } } async(balance()); /* 0x262bc77dfc5748620641325b3617fc07b20f0b92 Balance: 100000000000000000000 0x2d8785375886cf900126cf3082aad6f47427a1d7 Balance: 100000000000000000000 Total Balance: 200000000000000000000 */ ASYNC
  • 12. function balance() { try { $web3 = new Web3('http://localhost:8545'); $eth = $web3->eth; $accounts = $eth->accounts(); $total = new BigInteger(); foreach ($accounts as $account) { $balance = $eth->getBalance($account); echo $accounts . ' Balance: ' . $balance . PHP_EOL; $total = $total->add($balance); } echo 'Total Balance: ' . $total . PHP_EOL; return $total; } catch (Throwable $err) { echo 'Error: ' . $err->getMessage(); } } balance(); SYNC
  • 13. function balance(): Generator { try { $web3 = new Web3('http://localhost:8545'); $eth = $web3->eth; $accounts = yield [$eth, 'accounts']; $total = new BigInteger(); foreach ($accounts as $account) { $balance = yield [$eth, 'getBalance', $account]; echo $account . ' Balance: ' . $balance . PHP_EOL; $total = $total->add($balance); } echo 'Total Balance: ' . $total . PHP_EOL; return $total; } catch (Throwable $err) { echo 'Error: ' . $err->getMessage(); } } async(balance()); ASYNC