Better PHP Development.pdf - Google Drive

15 downloads 181 Views 2MB Size Report
JavaScript, PHP, design, and more. ii Better PHP Development. Page 3 of 151. Better PHP Development.pdf. Better PHP Deve
Better PHP Development

i

Better PHP Development Copyright © 2017 SitePoint Pty. Ltd. Cover Design: Natalia Balska

Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.

Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein.

Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark.

Published by SitePoint Pty. Ltd. 48 Cambridge Street Collingwood VIC Australia 3066 Web: www.sitepoint.com Email: [email protected]

ii Better PHP Development

About SitePoint SitePoint specializes in publishing fun, practical, and easy-to-understand content for web professionals. Visit http://www.sitepoint.com/ to access our blogs, books, newsletters, articles, and community forums. You’ll find a stack of information on JavaScript, PHP, design, and more.

Table of Contents iii

Table of Contents Preface .................................................................................v Chapter 1

How PHP Executes—from Source

Code to Render................................................................1 Chapter 2

Getting to Know and Love Xdebug8

Chapter 3

Localization Demystified: Php-Intl

for Everyone ...................................................................18 Chapter 4

Event Sourcing in a Pinch ...............30

Chapter 5

Disco with Design Patterns: A

Fresh Look at Dependency Injection ................63

iv Better PHP Development

Chapter 6

A Comprehensive Guide to Using

Cronjobs ............................................................................93 Chapter 7

Event Loops in PHP ...........................112

Chapter 8

PDO - the Right Way to Access

;"

There's a couple of noteworthy points from the above output. The first point is that not all pieces of the source code are named tokens. Instead, some symbols are considered tokens in and of themselves (such as =, ;, :, ?, etc). The second point is that the lexer actually does a little more than simply output a stream of tokens. It also, in most cases, stores the lexeme (the value matched by the token) and the line number of the matched token (which is used for things like stack traces).

Stage 2 - Parsing The parser is also generated, this time with Bison via a BNF grammar file. PHP uses a LALR(1) (look ahead, left-to-right) context-free grammar. The look ahead part simply means that the parser is able to look n tokens ahead (1, in this case) to resolve ambiguities it may encounter whilst parsing. The left-to-right part means that it parses the token stream from left-to-right. The generated parser stage takes the token stream from the lexer as input and has two jobs. It firstly verifies the validity of the token order by attempting to match them against any one of the grammar rules defined in its BNF grammar file. This ensures that valid language constructs are being formed by the tokens in the token stream. The second job of the parser is to generate the abstract syntax tree (AST) - a tree view of the source code that will be used during the next stage (compilation). We can view a form of the AST produced by the parser using the php-ast extension. The internal AST is not directly exposed because it is not particularly "clean" to work with (in terms of consistency and general usability), and so the

4 Better PHP Development php-ast extension performs a few transformations upon it to make it nicer to work with. Let's have a look at the AST for a rudimentary piece of code:

$code =