7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pimple 1.xと2.xの違い

7
Posted at

Pimple 2.xではshare()メソッドが削除され、サービスがデフォルトで共有されるようになった。

SilexなどPimpleに依存しているプログラムは、アップデートの際に注意が必要。

ただし修正は(たぶん)簡単。

Pimple 1.xの場合

$services = new Pimple();

// デフォルトではキーにアクセスするたび違うインスタンスが返却される.
$services['default'] = function ($services) {
    return new \stdClass();
};
var_dump($services['default'] === $services['default']); // false

// share()でサービス定義をラップすると、キーにアクセスするたび同じインスタンスが返却される.
$services['shared'] = $services->share(function ($services) {
    return new \stdClass();
});
var_dump($services['shared'] === $services['shared']); // true

Pimple 2.xの場合

$services = new Pimple();

// デフォルトではキーにアクセスするたび同じインスタンスが返却される.
$services['default'] = function ($services) {
    return new \stdClass();
};
var_dump($services['default'] === $services['default']); // true

// factory()でサービス定義をラップすると、キーにアクセスするたび違うインスタンスが返却される.
$services['monopolized'] = $services->factory(function ($services) {
    return new \stdClass();
});
var_dump($services['monopolized'] === $services['monopolized']); // false
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?