Giter VIP home page Giter VIP logo

zandstra's Introduction

PHP 8 Objects, Patterns, and Practice

Source Code

This directory contains the source code for PHP 8 Objects, Patterns, and Practice as well as some fairly rudimentary tests.

Running tests

In order to run the tests you will need to install PHPUnit. The easiest way to do this is to use composer (The composer.json and composer.lock files are also provided).

$ composer install

This will install PHPUnit locally. It will also configure autoload. You can then run the tests:

./vendor/bin/phpunit test/

Finding a code example

In the book, code examples are often presented as fragments or with details omitted. This is not the case in these files. Individual code examples are marked within runnable code using comments. For example here is an extract from src/ch13/batch01/SpaceMapper.php which contains code examples 13.16 and 13.17. This trick allows me to unit test the examples. I use some handrolled tools to generate and embed the code examples.

<?php

declare(strict_types=1);

namespace popp\ch13\batch01;

class SpaceMapper extends Mapper
{

    // ...

/* listing 13.16 */

        // SpaceMapper::__construct()

        $this->selectAllStmt = $this->pdo->prepare(
            "SELECT * FROM space"
        );
        $this->findByVenueStmt = $this->pdo->prepare(
            "SELECT * FROM space WHERE venue=?"
        );
/* /listing 13.16 */
    }

/* listing 13.17 */
    public function getCollection(array $raw): SpaceCollection
    {
        return new SpaceCollection($raw, $this);
    }
/* /listing 13.17 */

    protected function doCreateObject(array $raw): Space
    {
        $obj = new Space(
            (int)$raw['id'],
            $raw['name']
        );

    // ...

Of course, using comments makes it hard to locate an individual code example. For this reason I have included a contents file (named contents.txt). This is also autogenerated by a production tool, and should make it a breeze to find a particular example. Here for example is an extract that shows you how to find some examples in Chapter 13:

13.13: 
    src/ch13/batch01/Mapper.php
13.14: 
    src/ch13/batch01/SpaceMapper.php
13.15: 
    src/ch13/batch01/SpaceMapper.php
13.16: 
    src/ch13/batch01/SpaceMapper.php
13.17: 
    src/ch13/batch01/Venue.php
13.18: 
    src/ch13/batch03/Runner.php
13.19: 
    src/ch13/batch03/ObjectWatcher.php
13.20: 
    src/ch13/batch03/Mapper.php
13.21: 
    src/ch13/batch01/SpaceMapper.php

zandstra's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.