SlideShare a Scribd company logo
1 of 13
Download to read offline
THE CALLBACK HELL
a sneak peak!
Getting out of
“The situation where callbacks are
nested within other callbacks several
levels deep, potentially making it
difficult 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

PHPCon China 2016 - 從學徒變大師:談 Laravel 框架擴充與套件開發
PHPCon China 2016 - 從學徒變大師:談 Laravel 框架擴充與套件開發PHPCon China 2016 - 從學徒變大師:談 Laravel 框架擴充與套件開發
PHPCon China 2016 - 從學徒變大師:談 Laravel 框架擴充與套件開發Shengyou Fan
 
Spring Boot Actuator
Spring Boot ActuatorSpring Boot Actuator
Spring Boot ActuatorRowell Belen
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing optionsNir Kaufman
 
Introduction to Spring WebFlux #jsug #sf_a1
Introduction to Spring WebFlux #jsug #sf_a1Introduction to Spring WebFlux #jsug #sf_a1
Introduction to Spring WebFlux #jsug #sf_a1Toshiaki Maki
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKJosé Paumard
 
SwiftUI For Production | try! Swift 2019
SwiftUI For Production | try! Swift 2019SwiftUI For Production | try! Swift 2019
SwiftUI For Production | try! Swift 2019Lea Marolt Sonnenschein
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolvedtrxcllnt
 
Spring Cloud Function: Where We Were, Where We Are, and Where We’re Going
Spring Cloud Function: Where We Were, Where We Are, and Where We’re GoingSpring Cloud Function: Where We Were, Where We Are, and Where We’re Going
Spring Cloud Function: Where We Were, Where We Are, and Where We’re GoingVMware Tanzu
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90minsLarry Cai
 

What's hot (20)

PHPCon China 2016 - 從學徒變大師:談 Laravel 框架擴充與套件開發
PHPCon China 2016 - 從學徒變大師:談 Laravel 框架擴充與套件開發PHPCon China 2016 - 從學徒變大師:談 Laravel 框架擴充與套件開發
PHPCon China 2016 - 從學徒變大師:談 Laravel 框架擴充與套件開發
 
Spring Boot Actuator
Spring Boot ActuatorSpring Boot Actuator
Spring Boot Actuator
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Angular 2 observables
Angular 2 observablesAngular 2 observables
Angular 2 observables
 
Introduction to Spring WebFlux #jsug #sf_a1
Introduction to Spring WebFlux #jsug #sf_a1Introduction to Spring WebFlux #jsug #sf_a1
Introduction to Spring WebFlux #jsug #sf_a1
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Lombok
LombokLombok
Lombok
 
Basic Python Django
Basic Python DjangoBasic Python Django
Basic Python Django
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
Spring aop
Spring aopSpring aop
Spring aop
 
JavaScript Basic
JavaScript BasicJavaScript Basic
JavaScript Basic
 
Xke spring boot
Xke spring bootXke spring boot
Xke spring boot
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UK
 
SwiftUI For Production | try! Swift 2019
SwiftUI For Production | try! Swift 2019SwiftUI For Production | try! Swift 2019
SwiftUI For Production | try! Swift 2019
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolved
 
Spring Cloud Function: Where We Were, Where We Are, and Where We’re Going
Spring Cloud Function: Where We Were, Where We Are, and Where We’re GoingSpring Cloud Function: Where We Were, Where We Are, and Where We’re Going
Spring Cloud Function: Where We Were, Where We Are, and Where We’re Going
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
Spring boot
Spring bootSpring boot
Spring boot
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90mins
 

Similar to Getting out of Callback Hell in PHP

Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2markstory
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Elena 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 VersionIan Barber
 
Intro to php
Intro to phpIntro to php
Intro to phpSp 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
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPBradley Holt
 
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 Applicationselliando dias
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统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 CracowKacper Gunia
 
Zend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample QuestionsZend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample QuestionsJagat Kothari
 
DBD::SQLite
DBD::SQLiteDBD::SQLite
DBD::SQLitecharsbar
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebookguoqing75
 

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
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
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
 
Intro to php
Intro to phpIntro to php
Intro to php
 
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!
 
Swoole Overview
Swoole OverviewSwoole Overview
Swoole Overview
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
 
Symfony internals [english]
Symfony internals [english]Symfony internals [english]
Symfony internals [english]
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
Php Training Workshop by Vtips
Php Training Workshop by VtipsPhp Training Workshop by Vtips
Php Training Workshop by Vtips
 
Fatc
FatcFatc
Fatc
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
Php hacku
Php hackuPhp hacku
Php hacku
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
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
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
 
Zend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample QuestionsZend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample Questions
 
DBD::SQLite
DBD::SQLiteDBD::SQLite
DBD::SQLite
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
 

More from Arul Kumaran

Accelerating Xamarin Development
Accelerating Xamarin DevelopmentAccelerating Xamarin Development
Accelerating Xamarin DevelopmentArul Kumaran
 
iOS Native Development with Xamarin
iOS Native Development with XamariniOS Native Development with Xamarin
iOS Native Development with XamarinArul 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 APIArul 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.0Arul 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 AndroidArul Kumaran
 
UI Interactions Testing with FlexMonkey
UI Interactions Testing with FlexMonkeyUI Interactions Testing with FlexMonkey
UI Interactions Testing with FlexMonkeyArul Kumaran
 
Flex Production Tips & Techniques
Flex Production Tips & TechniquesFlex Production Tips & Techniques
Flex Production Tips & TechniquesArul Kumaran
 

More from Arul Kumaran (8)

Accelerating Xamarin Development
Accelerating Xamarin DevelopmentAccelerating Xamarin Development
Accelerating Xamarin Development
 
iOS Native Development with Xamarin
iOS Native Development with XamariniOS Native Development with Xamarin
iOS Native Development with Xamarin
 
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
 
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
 
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!
 
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
 
UI Interactions Testing with FlexMonkey
UI Interactions Testing with FlexMonkeyUI Interactions Testing with FlexMonkey
UI Interactions Testing with FlexMonkey
 
Flex Production Tips & Techniques
Flex Production Tips & TechniquesFlex Production Tips & Techniques
Flex Production Tips & Techniques
 

Recently uploaded

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 

Recently uploaded (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 

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 difficult 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