getAuditTrail() { return "nothing changed"; } } class Princess { use Audit; // General princess class description: // soldering, tree climbing, the usual } $daisy = new Princess(); echo $daisy->getAuditTrail();
used • There are rules about resolving naming clashes • Traits can include properties • Traits can include abstract methods • Traits can themselves make use of other traits
<?php if (file_exists(__DIR__ . '/' . $_SERVER['REQUEST_URI'])) { return false; // serve the requested resource as-is } else { include_once 'index.php'; } routing.php php -S localhost:8080 routing.php The webserver runs routing.php before entering the requested script. This example serves any existing resource, or routes to index.php
dust"; } $ping = function() { echo "ping!"; }; $pong = "pong"; echo sparkles($ping); // ping!fairy dust echo sparkles($pong); Catchable fatal error: Argument 1 passed to sparkles() must be callable, string given, called in /home/lorna/.../callable.php on line 16 and defined in /home/lorna/.../callable.php on line 10
object, PHP provides "magic methods" for us to hook into (this isn’t new): • __sleep() to specify which fields should be serialized • __wakeup() to perform any operations needed to complete an object when it is unserialized This gives us the same feature for when we JSON something
public mixed jsonSerialize () } Objects implementing the JsonSerializable interface can control how they are represented in JSON when they are passed to json_encode()