}; break; case 'RSHIFT': $this->functions[$wire] = function () use($parts) { $value = $this->getValue($parts[0]) >> $this->getValue($parts[2]); if (0 > $value) { $value += 65536; } return $value; }; break; } } } $handle = fopen("input.txt", "r"); $wires = new Wires(); if ($handle) { while (($line = fgets($handle)) !== false) { list($command, $wire) = explode(' -> ', trim($line)); $wires->add($wire, $command); } fclose($handle); } $allWires = $wires->getFunctions(); // Get the Signal of wire 'a' $signalOfA = $allWires['a'](); // Reset all the calculated values $wires->reset(); // Add a new wire 'b' with the signal of wire 'a' $wires->add('b', $signalOfA); // Get all the created functions again and calculate the signal of wire 'a'
<?php /** * Example #1 file demonstrating Wires * This example shows binding a parameter name to a class, * binding a parameter name to a value and context inheritance */ require_once dirname(__FILE__) . '/../../Wires.php'; $bindings = array('_global' => array('ExampleBase' => 'Example'), 'ExampleBase' => array('a' => array('class' => 'stdClass')), 'Example' => array('b' => array('value' => 42))); abstract class ExampleBase { function __construct($a) { $this->a = $a; } } class Example extends ExampleBase { function __construct($a, $b) { parent::__construct($a); $this->b = $b; } } $i = Wires::getInjector($bindings); $ex = $i->create('ExampleBase'); assert('$ex instanceof Example'); assert('$ex->a instanceof stdClass'); assert('$ex->b == 42');
$this->functions[$wire] = function () use($parts) { $value = $this->getValue($parts[0]) << $this->getValue($parts[2]); if (0 > $value) { $value += 65536; } return $value; }; break; case 'RSHIFT': $this->functions[$wire] = function () use($parts) { $value = $this->getValue($parts[0]) >> $this->getValue($parts[2]); if (0 > $value) { $value += 65536; } return $value; }; break; } } } $handle = fopen("input.txt", "r"); $wires = new Wires(); if ($handle) { while (($line = fgets($handle)) !== false) { list($command, $wire) = explode(' -> ', trim($line)); $wires->add($wire, $command); } fclose($handle); } $allWires = $wires->getFunctions(); echo 'Wire "A" has signal: ' . $allWires['a']() . PHP_EOL;