ebook img

Mastering PHP 7 PDF

514 Pages·2017·11.538 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Mastering PHP 7

Mastering PHP 7 Design, configure, build and test professional web applications in PHP 7 Branko Ajzele BIRMINGHAM - MUMBAI Mastering PHP 7 Copyright © 2017 Packt Publishing First published: June 2017 Production reference: 1220617 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78588-281-4 www.packtpub.com Contents Preface 1 Chapter 1: The All New PHP 7 Scalar type hints 8 Return type hints 11 Anonymous classes 12 Generator delegation 15 Generator return expressions 17 The null coalesce operator 18 The spaceship operator 20 Constant arrays 21 Uniform variable syntax 22 Throwables 24 Group use declarations 26 Catching multiple exceptions types 27 Class constant visibility modifiers 28 Iterable pseudo-type 29 Nullable types 30 Void return types 32 Summary 33 Chapter 2: Embracing Standards 34 PSR-1 - basic coding standard 36 PSR-2 - coding style guide 40 PSR-3 - logger interface 47 PSR-4 - autoloading standard 50 PSR-6 - caching interface 52 PSR-7 - HTTP message interface 54 PSR-13 - hypermedia links 59 Summary 61 Chapter 3: Error Handling and Logging 62 Error handling 63 Error 66 ArithmeticError 68 DivisionByZeroError 69 AssertionError 71 ParseError 72 TypeError 73 Uncaught error handler 75 Triggering errors 77 Exception 78 Creating a custom exception handler 79 Rethrowing exceptions 80 Uncaught Exception handler 81 Logging 82 Native logging 83 Logging with Monolog 86 Summary 91 Chapter 4: Magic Behind Magic Methods 92 Using __construct() 93 Using __destruct() 98 Using __call() 100 Using __callStatic() 103 Using __set() 104 Using __get() 106 Using __isset() 107 Using __unset() 109 Using __sleep() 110 Using __wakeup() 112 Using __toString() 114 Using __invoke() 116 Using __set_state() 117 Using __clone() 119 Using __debugInfo() 121 Usage statistics across popular platforms 121 Summary 123 Chapter 5: The Realm of CLI 124 Understanding PHP CLI 125 The Console component 127 Setting up the Console component 128 Creating a console command 131 Dealing with inputs 134 Using Console component helpers 138 Input/output streams 140 Process control 142 Ticks 143 Signals 146 Alarms 149 Multiprocessing 151 Summary 154 Chapter 6: Prominent OOP Features 155 Object inheritance 156 Objects and references 161 Object iteration 163 Object comparison 166 Traits 168 Reflection 174 Summary 178 Chapter 7: Optimizing for High Performance 179 Max execution time 180 Memory management 182 File uploads 184 Session handling 187 Output buffering 189 Disabling debug messages 192 Zend OPcache 196 Concurrency 197 Summary 198 Chapter 8: Going Serverless 199 Using the serverless framework 200 Using Iron.io IronWorker 209 Summary 223 Chapter 9: Reactive Programming 224 Similarities with event-driven programming 225 Using RxPHP 227 Installing RxPHP 227 Observable and observer 229 Subject 234 Operator 236 Writing custom operators 241 Non-blocking IO 244 Using React 250 Installing React 250 React event loop 252 Observables and event loop 256 Summary 259 Chapter 10: Common Design Patterns 260 Base patterns 261 The registry pattern 261 Creational patterns 262 The singleton pattern 263 The prototype pattern 264 The abstract factory pattern 265 The builder pattern 267 The object pool pattern 269 Behavioral patterns 271 The strategy pattern 271 The observer pattern 272 The lazy initialization pattern 274 The chain of responsibility pattern 276 Structural patterns 277 The decorator pattern 278 Summary 280 Chapter 11: Building Services 281 Understanding the client-server relationship 282 Working with SOAP 282 XML extensions 284 Creating server 289 Creating WSDL file 292 Creating client 296 Working with REST 298 JSON extensions 299 Creating server 300 Creating client 304 Working with Apache Thrift (RPC) 306 Installing Apache Thrift 306 Defining service 308 Creating server 311 Creating client 312 Understanding microservices 313 Summary 315 Chapter 12: Working with Databases 316 Working with MySQL 317 Installing MySQL 318 Setting up sample data 322 Querying via the MySQLi driver extension 325 Connecting 325 Error handling 325 Selecting 326 Binding parameters 329 Inserting 330 Updating 332 Deleting 333 Transactions 333 Querying via the PHP Data Objects driver extension 335 Connecting 336 Error handling 336 Selecting 337 Inserting 340 Updating 341 Deleting 341 Transactions 342 Working with MongoDB 343 Installing MongoDB 344 Setting up sample data 344 Querying via the MongoDB driver extension 345 Connecting 346 Error handling 347 Selecting 347 Inserting 349 Updating 350 Deleting 352 Transactions 353 Working with Redis 354 Installing Redis 354 Setting up sample data 355 Querying via the phpredis driver extension 356 Connecting 357 Error handling 357 Selecting 357 Inserting 359 Updating 361 Deleting 361 Transactions 362 Summary 363 Chapter 13: Resolving Dependencies 364 Mitigating the common problem 365 Understanding dependency injection 367 Understanding dependency injection container 371 Summary 374 Chapter 14: Working with Packages 375 Understanding Composer 376 Understanding Packagist 378 Using third-party packages 380 Creating your own package 382 Distributing your package 385 Summary 389 Chapter 15: Testing the Important Bits 390 PHPUnit 391 Setting up the PHPUnit 392 Setting up a sample application 393 Writing test 398 Executing tests 402 Code coverage 403 Behat 406 Setting up Behat 407 Setting up a sample application 408 Writing test 410 Executing tests 416 phpspec 418 Setting up phpspec 418 Writing test 419 Executing tests 423 jMeter 426 Writing test 426 Executing tests 433 Summary 434 Chapter 16: Debugging, Tracing, and Profiling 435 Xdebug 436 Installation 437 Debugging 441 Tracing 449 Profiling 451 Zend Z-Ray 453 Installing Zend Server 454 Setting up the virtual host 460 Using Z-Ray 466 Summary 469 Chapter 17: Hosting, Provisioning, and Deployment 470 Choosing the right hosting plan 471 Shared server 471 Virtual private server 472 Dedicated server 474 PaaS 475 Automating provisioning 478 Setting up the workstation machine 480 Setting up the server machine 481 Configuring Ansible 484 Provisioning a web server 486 Automating the deployment 487 Installing Deployer 488 Using Deployer 489 Continuous integration 495 Jenkins 496 Summary 497 Index 499 Preface The PHP language has been around for quite some time now. What started out as a humble set of scripts, soon turned into a powerful scripting language. The rise of various frameworks and platforms paved the way into the hearts of many developers. Over time, PHP coding standards sprung out, along with numerous testing solutions. These gave it the solid enterprise foothold it has today. The latest PHP 7.1 release brings forth enormous amount of improvements, both from the language syntax and overall performance perspective. There has never been a better time to dig into a PHP than now. Throughout this book, we will be covering a wide range of topics. These might seem seemingly random at first, but they reflect a minimum skill level PHP developers are required to possess nowadays. What this book covers Chapter 1, The All New PHP, talks about the latest changes introduced to the PHP 7.1 language, most of which directly improve the quality and elegancy of written code. Chapter 2, Embracing Standards, introduces you to important standards in the PHP ecosystem. Presented standards affect the quality and elegancy of code, pushing ypu closer to truly mastering PHP. Chapter 3, Error Handling and Logging, stresses on the importance of robust error handling and effective logging. You will learn how to handle errors and log truly important bits of information--two disciplines that often lack proper attention in everyday PHP coding. Chapter 4, Magic Behind Magic Methods, discusses the magic functions available in PHP classes, and their beauty and importance. You will learn every PHP magic method, and its meaning and use through practical examples. Chapter 5, The Realm of CLI, explores command-line PHP, and its tools and processes. You will learn how to use Symfony's Console component, work with input/output streams, and handle processes.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.