This package is abandoned and no longer maintained. No replacement package was suggested.

v1.0.0 2017-01-24 07:51 UTC


The FileContents component provides basic utilities to remplace file_put_contents and file_get_contents.

Example #1 Simple usage example

$filecontents = new Methylbro\File\FileContents();

// Open the file to get existing content
$file = 'people.txt';
$current = $filecontents->get($file);

// Append a new person to the file
$current .= "John Smith\n";

// Write the contents back to the file
$filecontents->put($file, $current);

Example #2 Using flags

$filecontents = new Methylbro\File\FileContents();
$file = 'people.txt';

// The new person to add to the file
$person = "John Smith\n";

// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
$filecontents->put($file, $person, FILE_APPEND | LOCK_EX);


composer require methylbro/filecontents


If you use file_put_content or file_get_contents function somewhere in your code it could became hard to test it. Instead you should use FileContents so you could mock these things.

use Methylbro\File\FileContents;

class HelloWorld
    private $filecontents;

    public function __construct(FileContents $filecontents)
        $this->filecontents = $filecontents;

    public function getMessage($source)
        return $this->filecontents->get($source);
use PHPUnit\Framework\TestCase;
use Methylbro\File\FileContents;

class HelloWorldTest extends \PHPUnit\Framework\TestCase
    public function testPrint()
        $message = "hello world";

        $filecontents = $this

        $hello_world = new HelloWorld($filecontents);

        $this->assertEquals($message, $hello_world->getMessage());