using-extension-methods.php 706 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. if (@!include __DIR__ . '/../vendor/autoload.php') {
  3. die('Install dependencies using `composer install --dev`');
  4. }
  5. Tracy\Debugger::enable();
  6. ?>
  7. <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
  8. <h1>Using Extension Methods | Dibi</h1>
  9. <?php
  10. $dibi = new Dibi\Connection([
  11. 'driver' => 'sqlite3',
  12. 'database' => 'data/sample.s3db',
  13. ]);
  14. // using the "prototype" to add custom method to class Dibi\Result
  15. Dibi\Result::extensionMethod('fetchShuffle', function (Dibi\Result $obj) {
  16. $all = $obj->fetchAll();
  17. shuffle($all);
  18. return $all;
  19. });
  20. // fetch complete result set shuffled
  21. $res = $dibi->query('SELECT * FROM [customers]');
  22. $all = $res->fetchShuffle();
  23. Tracy\Dumper::dump($all);