PHPackages                             gonzalo123/using - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gonzalo123/using

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gonzalo123/using
================

c# using statement implementation in PHP

119[1 issues](https://github.com/gonzalo123/using/issues)PHP

Since Jun 21Pushed 10y ago2 watchersCompare

[ Source](https://github.com/gonzalo123/using)[ Packagist](https://packagist.org/packages/gonzalo123/using)[ RSS](/packages/gonzalo123-using/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

implementation of C# "using" statement in PHP

[![Build Status](https://camo.githubusercontent.com/9a1a7cef9a1d656dbce1531aefc1d386b6c8d6ab8d05b8af631509cd19e899b3/68747470733a2f2f7472617669732d63692e6f72672f676f6e7a616c6f3132332f7573696e672e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/gonzalo123/using)[![Latest Stable Version](https://camo.githubusercontent.com/a6b9bfe8534bbb496acd2b413c03596ca5812b827533fb944ad2c77d95f4ccbe/68747470733a2f2f706f7365722e707567782e6f72672f676f6e7a616c6f3132332f7573696e672f762f737461626c652e706e67)](https://packagist.org/packages/gonzalo123/using)

Usage
-----

[](#usage)

```
using(new File(__DIR__ . "/file.txt", 'w'), function (File $file) {
        $file->write("Hello\n");
        $file->write("Hello\n");
        $file->write("Hello\n");
    });
```

The problem
-----------

[](#the-problem)

Imagine this class:

```
class File
{
    private $resource;

    public function __construct($filename, $mode)
    {
        $this->resource = fopen($filename, $mode);
    }

    public function write($string)
    {
        fwrite($this->resource, $string);
    }

    public function close()
    {
        fclose($this->resource);
    }
}
```

We can use this class:

```
$file = new File(__DIR__ . "/file.txt", 'w');
$file->write("Hello\n");
// ...
// some other things
// ...
$file->write("Hello\n");
$file->close();
```

What happens if there is an exceptions in "some other things"? Simple: close() function isn't called.

The solution
------------

[](#the-solution)

We can solve the problem with try - catch:

```
try {
    $file->write("Hello\n");
    // ...
    // some other things
    // ...
    $file->write("Hello\n");
    $file->close();
} catch (\Exception $e) {
    $file->close();
}
```

or using "finally" keyword is we use PHP5.5

```
try {
    $file->write("Hello\n");
    // ...
    // some other things
    // ...
    $file->write("Hello\n");
} catch (\Exception $e) {
} finally {
    $file->close();
}
```

Better solution
===============

[](#better-solution)

c# has "using" statement to solve this problem in a smart way.

We're going to implement something similar in PHP.

First we will add G\\IDisposable interface to our File class

```
namespace G;

interface IDisposable
{
    public function dispose();
}
```

Now our File class looks like this:

```
class File implements IDisposable
{
    private $resource;

    public function __construct($filename, $mode)
    {
        $this->resource = fopen($filename, $mode);
    }

    public function write($string)
    {
        fwrite($this->resource, $string);
    }

    public function close()
    {
        fclose($this->resource);
    }

    public function dispose()
    {
        $this->close();
    }
}
```

And we can use our "using" funcion in PHP:

```
using(new File(__DIR__ . "/file.txt", 'w'), function (File $file) {
        $file->write("Hello\n");
        $file->write("Hello\n");
        $file->write("Hello\n");
    });
```

As we can see we can forget to close() our file instance. "using" will do it for us, even if one exception is triggered inside.

We also can use an array of instances (implementing the IDisposable interface of course)

```
using([new Bar, new Foo], function (Bar $bar, Foo $foo) {
        echo $bar->hello("Gonzalo");
        echo $foo->hello("Gonzalo");
    });
```

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/c5bcdeee0f7e214945e0d27310df0ad487e7d1ec10a8d4341dbdcabfac58f2f7?d=identicon)[gonzalo123](/maintainers/gonzalo123)

---

Top Contributors

[![gonzalo123](https://avatars.githubusercontent.com/u/39072?v=4)](https://github.com/gonzalo123 "gonzalo123 (12 commits)")

### Embed Badge

![Health badge](/badges/gonzalo123-using/health.svg)

```
[![Health](https://phpackages.com/badges/gonzalo123-using/health.svg)](https://phpackages.com/packages/gonzalo123-using)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
