mirror of
https://github.com/dg/dibi.git
synced 2025-08-06 14:16:39 +02:00
Strict: extension methods are deprecated
This commit is contained in:
@@ -1,34 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
if (@!include __DIR__ . '/../vendor/autoload.php') {
|
|
||||||
die('Install dependencies using `composer install --dev`');
|
|
||||||
}
|
|
||||||
|
|
||||||
Tracy\Debugger::enable();
|
|
||||||
|
|
||||||
?>
|
|
||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
|
||||||
|
|
||||||
<h1>Using Extension Methods | dibi</h1>
|
|
||||||
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$dibi = new Dibi\Connection([
|
|
||||||
'driver' => 'sqlite3',
|
|
||||||
'database' => 'data/sample.s3db',
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
|
||||||
// using the "prototype" to add custom method to class Dibi\Result
|
|
||||||
Dibi\Result::extensionMethod('fetchShuffle', function (Dibi\Result $obj) {
|
|
||||||
$all = $obj->fetchAll();
|
|
||||||
shuffle($all);
|
|
||||||
return $all;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// fetch complete result set shuffled
|
|
||||||
$res = $dibi->query('SELECT * FROM [customers]');
|
|
||||||
$all = $res->fetchShuffle();
|
|
||||||
Tracy\Dumper::dump($all);
|
|
@@ -29,7 +29,9 @@ trait Strict
|
|||||||
*/
|
*/
|
||||||
public function __call($name, $args)
|
public function __call($name, $args)
|
||||||
{
|
{
|
||||||
if ($cb = self::extensionMethod(get_class($this) . '::' . $name)) { // back compatiblity
|
$class = get_class($this);
|
||||||
|
if ($cb = self::extensionMethod($class . '::' . $name)) { // back compatiblity
|
||||||
|
trigger_error("Extension methods such as $class::$name() are deprecated", E_USER_DEPRECATED);
|
||||||
array_unshift($args, $this);
|
array_unshift($args, $this);
|
||||||
return $cb(...$args);
|
return $cb(...$args);
|
||||||
}
|
}
|
||||||
@@ -104,6 +106,7 @@ trait Strict
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
public static function extensionMethod(string $name, callable $callback = null)
|
public static function extensionMethod(string $name, callable $callback = null)
|
||||||
{
|
{
|
||||||
@@ -129,6 +132,7 @@ trait Strict
|
|||||||
return $cache = false;
|
return $cache = false;
|
||||||
|
|
||||||
} else { // setter
|
} else { // setter
|
||||||
|
trigger_error("Extension methods such as $class::$name() are deprecated", E_USER_DEPRECATED);
|
||||||
$list[$class] = $callback;
|
$list[$class] = $callback;
|
||||||
$list[''] = null;
|
$list[''] = null;
|
||||||
}
|
}
|
||||||
|
@@ -148,12 +148,3 @@ Assert::exception(function () {
|
|||||||
}, LogicException::class, 'Attempt to unset undeclared property TestClass::$undeclared.');
|
}, LogicException::class, 'Attempt to unset undeclared property TestClass::$undeclared.');
|
||||||
|
|
||||||
Assert::false(isset($obj->undeclared));
|
Assert::false(isset($obj->undeclared));
|
||||||
|
|
||||||
|
|
||||||
// extension method
|
|
||||||
TestClass::extensionMethod('join', $func = function (TestClass $that, $separator) {
|
|
||||||
return $that->foo . $separator . $that->bar;
|
|
||||||
});
|
|
||||||
|
|
||||||
$obj = new TestClass;
|
|
||||||
Assert::same('456*123', $obj->join('*'));
|
|
||||||
|
Reference in New Issue
Block a user