PHP Crash Course The Complete, Modern, Hands-On Guide (Matt Smith) (Z-Library)

Author: Matt Smith

科学

From First Script to Full Website—Fast! Tired of cobbling together PHP solutions from scattered online tutorials? Frustrated by outdated PHP practices that leave your code vulnerable and hard to maintain? Whether you’re building your first dynamic website or modernizing legacy systems, PHP Crash Course gives you a complete, practical foundation for writing professional web applications. In this comprehensive, example-driven guide, you’ll learn how to: - Write clean, maintainable PHP code using modern language features and best practices - Build secure web applications that protect against common vulnerabilities - Master database integration using PDO and object-relational mappings (ORMs) - Implement professional features like shopping carts and user authentication - Structure applications using object-oriented programming and model-view-controller (MVC) patterns - Leverage powerful tools like Composer and Twig to accelerate development Starting with PHP fundamentals, you’ll progress through six carefully crafted sections covering essential patterns, security best practices, database integration, and advanced concepts like object-oriented programming. Each chapter builds on real-world examples, giving you the skills to solve common development challenges. Whether you’re a complete beginner or an experienced developer looking to modernize your PHP skills, PHP Crash Course gives you everything you need to build professional, dynamic websites with confidence. Includes setup instructions for Windows, macOS, and Linux, and configuration details for Replit, the free online development environment.

📄 File Format: PDF
💾 File Size: 18.2 MB
23
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
® T H E C O M P L E T E , M O D E R N , H A N D S - O N G U I D E M A T T S M I T H P H P C R A S H C O U R S E THE F INEST IN GEEK ENTERTA INMENT ™ nostarch.com ® ® P H P C R A S H C O U R S E S M I T H $69.99 US ($91.99 CDN) Covers PHP 8.x FROM FIRST SCRIPT TO FULL WEBSITE—FAST! Tired of cobbling together PHP solutions from scattered online tutorials? Frustrated by outdated PHP practices that leave your code vulnerable and hard to maintain? Whether you’re building your fi rst dynamic website or modernizing legacy systems, PHP Crash Course gives you a complete, practical foundation for writing profes- sional web applications. In this comprehensive, example-driven guide, you’ll learn how to: • Write clean, maintainable PHP code using modern language features and best practices • Build secure web applications that protect against common vulnerabilities • Master database integration using PDO and object- relational mappings (ORMs) • Implement professional features like shopping carts and user authentication • Structure applications using object-oriented programming and model-view-controller (MVC) patterns • Leverage powerful tools like Composer and Twig to accelerate development Starting with PHP fundamentals, you’ll progress through six carefully crafted sections covering essential patterns, security best practices, database integration, and advanced concepts like object-oriented programming. Each chapter builds on real-world examples, giving you the skills to solve common development challenges. Whether you’re a complete beginner or an experienced developer looking to modernize your PHP skills, PHP Crash Course gives you everything you need to build professional, dynamic websites with confi dence. A B O U T T H E A U T H O R Dr. Matt Smith is a senior lecturer in computing at Technological University Dublin, specializing in web applications and immersive technologies. With over 30 years of teaching experience and degrees from the University of Huddersfi eld (BA), the University of Aberdeen (MSc), and the Open University (PhD), he has been at the forefront of PHP-based web development education since the mid-2000s. Includes setup instructions for Windows, macOS, and Linux, and confi guration details for Replit, the free online development environment.
📄 Page 2
(This page has no text content)
📄 Page 3
PHP CRASH COURSE
📄 Page 4
(This page has no text content)
📄 Page 5
® P H P C R A S H C O U R S E T h e C o m p l e t e , M o d e r n , H a n d s - o n G u i d e by Matt Smith San Francisco
📄 Page 6
PHP CRASH COURSE. Copyright © 2025 by Matt Smith. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. First printing ISBN-13: 978-1-7185-0252-9 (print) ISBN-13: 978-1-7185-0253-6 (ebook) Published by No Starch Press®, Inc. 245 8th Street, San Francisco, CA 94103 phone: +1 .415 .863 .9900 www .nostarch .com; info@nostarch .com Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Sabrina Plomitallo-González Production Editor: Sydney Cromwell Developmental Editor: Nathan Heidelberger Cover Illustrator: Gina Redman Interior Design: Octopod Studios Technical Reviewers: Ryan Weaver and Eoghan Ó hUallacháin Copyeditor: Sharon Wilkey Proofreader: Daniel Wolff Figure 28-1 is reproduced with permission from https://xkcd .com /327/. Library of Congress Cataloging-in-Publication Data Name: Smith, Matt, 1967-author. Title: PHP Crash Course : The Complete, Modern, Hands-on Guide / by Matt Smith. Description: San Francisco : No Starch Press, 2025. | Includes index. Identifiers: LCCN 2024028603 (print) | LCCN 2024028604 (ebook) | ISBN 9781718502529 (print) | ISBN 9781718502536 (ebook) Subjects: LCSH: PHP (Computer program language) | Internet programming—Computer programs. | Computer programming. Classification: LCC QA76.73.P224 S566 2025 (print) | LCC QA76.73.P224 (ebook) | DDC 005.13/3—dc23/eng20241007 LC record available at https://lccn.loc.gov/2024028603 LC ebook record available at https://lccn.loc.gov/2024028604 For customer service inquiries, please contact info@nostarch .com. For information on distribution, bulk sales, corporate sales, or translations: sales@nostarch .com. For permission to translate this work: rights@nostarch .com. To report counterfeit copies or piracy: counterfeit@nostarch .com. No Starch Press and the No Starch Press iron logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. ®
📄 Page 7
I dedicate this book to Ryan Weaver. Having this book reviewed by someone of your experience and expertise has been a fantastic privilege. Working with a core Symfony developer has helped ensure that the content is accurate and reflects professional PHP best practices. Quite simply, the book is better than it would have been without your input. My sincere thanks and best wishes to you and your family.
📄 Page 8
(This page has no text content)
📄 Page 9
About the Author Dr. Matt Smith is senior lecturer in computing at Technological University (TU) Dublin, Ireland, specializing in interactive multimedia instruc- tion including web applications, extended reality (XR), and e-learning systems. He holds a bachelor’s degree in business computing (University of Huddersfield, UK), a master’s in artificial intelligence (University of Aberdeen, Scotland), and a PhD in computational musicology (Open University, UK). He has been teaching for over 30 years, holding full- time positions at the University of Winchester and London’s Middlesex University prior to TU Dublin. Smith introduced Unity-based 3D game development and interactive virtual environments to his computing and digital media students. In the mid-2000s, at the request of his students, he switched the focus of his web development courses from Java to PHP, and he’s been a fan of PHP and the Symfony open source web framework ever since. Smith has a first-degree black belt in Taekwondo and has taught and competed in that martial art, though when his daughter switched to Shotokan karate, so did he, and he hopes to earn his first-degree karate black belt at about the time this book is published. While at school in the 1980s, Smith wrote the lyrics for, and his band played the music on, the B-side of the audio cassette carrying the computer game Confuzion by Incentive Software; the game has a Wikipedia page. He also sang some of the backup vocals, for which he apologizes. About the Technical Reviewers Ryan Weaver is a member of the Symfony core team and a writer for SymfonyCasts. He has contributed to and created numerous open source PHP packages. He lives in Grand Rapids, Michigan, with his wife, Leanna, and son, Beckett. Eoghan Ó hUallacháin is the founder and CEO of Glorsoft, a software engineering company specializing in architecting and developing high- availability, high-volume, multiuser, speed-critical systems. He has over 30 years of experience delivering highly scalable systems for clients in e-commerce, telecoms, finance, government, and other sectors. He’s been writing PHP code since 1999 and uses it for user interface and automation work. He holds a BS in computer applications from Dublin City University and an MS in advanced software engineering from University College Dublin.
📄 Page 10
(This page has no text content)
📄 Page 11
B R I E F C O N T E N T S Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxv PART I: LANGUAGE FUNDAMENTALS . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1: PHP Program Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Chapter 3: Strings and String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Chapter 4: Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Chapter 5: Custom Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 PART II: WORKING WITH DATA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109 Chapter 6: Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Chapter 7: Simple Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Chapter 8: Sophisticated Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Chapter 9: Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 PART III: PROGRAMMING WEB APPLICATIONS . . . . . . . . . . . . . . . . . . .175 Chapter 10: Client/Server Communication and Web Development Basics . . . . . . . . . . . . 177 Chapter 11: Creating and Processing Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Chapter 12: Validating Form Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Chapter 13: Organizing a Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 PART IV: STORING USER DATA WITH BROWSER SESSIONS . . . . . . . . . . .259 Chapter 14: Working with Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Chapter 15: Implementing a Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Chapter 16: Authentication and Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
📄 Page 12
x   Brief Contents PART V: OBJECT-ORIENTED PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325 Chapter 17: Introduction to Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . 327 Chapter 18: Declaring Classes and Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Chapter 19: Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 Chapter 20: Managing Classes and Namespaces with Composer . . . . . . . . . . . . . . . . . . 381 Chapter 21: Efficient Template Design with Twig . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 Chapter 22: Structuring an Object-Oriented Web Application . . . . . . . . . . . . . . . . . . . . . 427 Chapter 23: Error Handling with Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 Chapter 24: Logging Events, Messages, and Transactions . . . . . . . . . . . . . . . . . . . . . . . . 459 Chapter 25: Static Methods, Properties, and Enumerations . . . . . . . . . . . . . . . . . . . . . . . 475 Chapter 26: Abstract Methods, Interfaces, and Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 PART VI: DATABASE-DRIVEN APPLICATION DEVELOPMENT . . . . . . . . . . .529 Chapter 27: Introduction to Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531 Chapter 28: Database Programming with the PDO Library . . . . . . . . . . . . . . . . . . . . . . . 541 Chapter 29: Programming CRUD Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 Chapter 30: ORM Libraries and Database Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595 Chapter 31: Working with Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 Appendix A: Installing PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 Appendix B: Database Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669 Appendix C: Replit Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
📄 Page 13
C O N T E N T S I N D E T A I L ACKNOWLEDGMENTS xxiii INTRODUCTION xxv Who This Book Is For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi Why PHP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi What You Will Learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxix PART I: LANGUAGE FUNDAMENTALS 1 1 PHP PROGRAM BASICS 3 Two Methods to Run PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Replit Online Coding Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 A Local PHP Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Template Text vs . PHP Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Creating Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Operators and Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Combined Arithmetic Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . 22 Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2 DATA TYPES 27 PHP Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Scalar Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 The Special NULL Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Functions to Test for a Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Type Juggling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Numeric Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 String Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Comparative Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Logical and Other Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Type Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
📄 Page 14
xii   Contents in Detail 3 STRINGS AND STRING FUNCTIONS 41 Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Single-Quoted Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Joining Strings: Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Double-Quoted Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Handling the Character After a Variable Name . . . . . . . . . . . . . . . . . . . . . . 47 Incorporating Unicode Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Heredocs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Indention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Nowdocs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Built-in String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Converting to Upper- and Lowercase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Searching and Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Extracting and Replacing Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Trimming Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Removing All Unnecessary Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Repeating and Padding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 4 CONDITIONALS 65 Conditions Are True or False . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 if . . .else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Nested if . . .else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 if . . .elseif . . .else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Alternative Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 NOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 AND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 OR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 XOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 match Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 The Ternary Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 The Null-Coalescing Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5 CUSTOM FUNCTIONS 81 Separating Code into Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Reading in and Executing Another Script . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Creating Absolute Filepaths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Declaring and Calling a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Parameters vs . Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Errors from Incorrect Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Type Juggling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
📄 Page 15
Contents in Detail   xiii Functions Without Explicit Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Returning NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Exiting a Function Early . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Calling Functions from Within Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Functions with Multiple Return and Parameter Types . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Nullable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Union Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Optional Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Positional vs . Named Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Skipped Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Pass-by-Value vs . Pass-by-Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 PART II: WORKING WITH DATA 109 6 LOOPS 111 while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 do . . .while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Boolean Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 break Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Using the Counter in the Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Skipping Loop Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Handling the Last Iteration Differently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Alternative Loop Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Avoiding Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 7 SIMPLE ARRAYS 125 Creating an Array and Accessing Its Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Updating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Appending an Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Adding an Element with a Specific Key . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Appending Multiple Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Removing the Last Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Retrieving Information About an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Looping Through an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Using a foreach Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Accessing Keys and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Imploding an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Functions with a Variable Number of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Array Copies vs . Array References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Treating Strings as Arrays of Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Other Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
📄 Page 16
xiv   Contents in Detail 8 SOPHISTICATED ARRAYS 143 Declaring Array Keys Explicitly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Arrays with Strings as Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 More Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Removing Any Element from an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Combining and Comparing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Destructuring an Array into Multiple Variables . . . . . . . . . . . . . . . . . . . . . . 152 Callback Functions and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 9 FILES AND DIRECTORIES 157 Reading a File into a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Confirming That a File Exists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 “Touching” a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Ensuring That a Directory Exists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Writing a String to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Managing Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Reading a File into an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Using Lower-Level File Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Processing Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 JSON and Other File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 PART III: PROGRAMMING WEB APPLICATIONS 175 10 CLIENT/SERVER COMMUNICATION AND WEB DEVELOPMENT BASICS 177 The HTTP Request-Response Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Response Status Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 An Example GET Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 How Servers Operate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Simple Web Servers for File Retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Dynamic Web Servers for Processing Data . . . . . . . . . . . . . . . . . . . . . . . . . 183 The Routing Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Templating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 PHP Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Short Echo Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 The Model-View-Controller Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Structuring a PHP Web Development Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
📄 Page 17
Contents in Detail   xv 11 CREATING AND PROCESSING WEB FORMS 195 Basic Client/Server Communication for Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . 196 GET vs . POST Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 The filter_input() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Other Ways to Send Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Sending Noneditable Data Along with Form Variables . . . . . . . . . . . . . . . . 206 Processing Mixed Query-String and POST Variables . . . . . . . . . . . . . . . . . . 207 Offering Multiple Submit Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Encoding Data in Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Other Form Input Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Checkboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Single-Selection Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Multiple-Selection Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 12 VALIDATING FORM DATA 225 Writing Custom Validation Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Managing Multiple Validation Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Testing for a Valid Zero Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Displaying and Validating Forms in a Single Postback Script . . . . . . . . . . . . . . . . . . . 230 Simple Validation Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Array-Based Validation Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 13 ORGANIZING A WEB APPLICATION 239 Front Controllers and the MVC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Separating Display and Logic Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Creating the Front Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Writing the Display Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Moving Website Logic into Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Designing a Secure Folder Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Simplifying the Front-Controller Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Writing the Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Generalizing the Front-Controller Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Distinguishing Between Requested Pages . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Building a Multipage Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
📄 Page 18
xvi   Contents in Detail PART IV: STORING USER DATA WITH BROWSER SESSIONS 259 14 WORKING WITH SESSIONS 261 A Web Browser Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 The session_start() and session_id() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 The $_SESSION Superglobal Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Updating a Stored Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Unsetting a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Destroying the Session and Emptying the Session Array . . . . . . . . . . . . . . . . 270 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 15 IMPLEMENTING A SHOPPING CART 275 The Shopping Cart File Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Defining the Product List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Creating the Products Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Adding CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Displaying the Star Ratings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Creating the Template Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Updating the Index Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Designing the Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Creating the Front Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Managing the Product and Cart Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Streamlining the Index Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Creating a Header Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Creating the Cart Display Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 Interacting with the Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Updating the Cart-Retrieval Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Implementing Cart-Manipulation Functions . . . . . . . . . . . . . . . . . . . . . . . . . 293 Creating the Empty Cart Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Finalizing the Front Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 Adding Display Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Writing the switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 16 AUTHENTICATION AND AUTHORIZATION 301 A Simple Login Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 Creating a Site with a Login Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Defining the File Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Creating the Shared Page Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Designing the Page Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Developing the Login Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 Writing the Front Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
📄 Page 19
Contents in Detail   xvii Implementing the Logic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 Creating the Error Page Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Storing Login Data with Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Updating the Front Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Writing the Login Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Updating the Header Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Updating the Banking Page Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Offering a Logout Feature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Adding the Logout Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 Updating the Front Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 Displaying the Logout Link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 Displaying the Logged-in Username . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Retrieving the Username . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Updating the Navigation Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Updating the CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 PART V: OBJECT-ORIENTED PHP 325 17 INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING 327 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 Creating Relationships Between Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 Encapsulation and Information Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Superclasses, Inheritance, and Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 The Flow of Control for Object-Oriented Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 An Example Class Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 18 DECLARING CLASSES AND CREATING OBJECTS 337 Declaring a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Creating an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Private Properties with Public Accessor Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 Getting and Setting Private Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Screening for Invalid Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Overriding Default Class Behavior with Magic Methods . . . . . . . . . . . . . . . . . . . . . . 346 Initializing Values with a Constructor Method . . . . . . . . . . . . . . . . . . . . . . . 346 Converting Objects to Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Object Variables as References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 Handling Missing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 Custom Methods and Virtual Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
📄 Page 20
xviii   Contents in Detail 19 INHERITANCE 357 Inheritance as Generalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Creating Objects from Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Using Multiple Levels of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Protected Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Overriding Inherited Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368 Augmenting Inherited Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Preventing Subclassing and Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Declaring a Class final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Declaring a Method final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 20 MANAGING CLASSES AND NAMESPACES WITH COMPOSER 381 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Declaring a Class’s Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Using a Namespaced Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 Referencing Namespaces in Class Declarations . . . . . . . . . . . . . . . . . . . . . 384 Composer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Installing and Testing Composer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Creating the composer .json Configuration File . . . . . . . . . . . . . . . . . . . . . . 386 Creating an Autoloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 Adding Third-Party Libraries to a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 Where to Find PHP Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 21 EFFICIENT TEMPLATE DESIGN WITH TWIG 395 The Twig Templating Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 How Twig Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 Manipulating Objects and Arrays in Twig Templates . . . . . . . . . . . . . . . . . . . . . . . . . 403 Twig Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 Creating a Multipage Website with Twig . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 The File Structure and Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 The Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 The Twig Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 Twig Features to Improve Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 Improved Page Styling with CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 22 STRUCTURING AN OBJECT-ORIENTED WEB APPLICATION 427 Separating Display and Front-Controller Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428 Using Multiple Controller Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List