dimanche 10 novembre 2019

Is it acceptable for a value object to read and store the contents of a file?

I've been experimenting with the concept of "Value objects" and I was wondering if it is considered "good practice" to read the contents of a file in the constructor of a value objects, like so:

<?php

class ValueObject {

    private array $content;

    public function __construct(string $path)
    {
        if (file_exists($path) === false) {
            throw new Exception('The file does not exist.');
        }

        $this->content = require $path;
    }

    public function getContent(): array
    {
        return $this->content;
    }

}

Or is it better to pass the content of a file explicitly to the constructor and read the content of a file somewhere else:

<?php

class ValueObject {

    private array $content;

    public function __construct(array $content)
    {
        $this->content = $content;
    }

    public function getContent(): array
    {
        return $this->content;
    }

}

Thanks!

Aucun commentaire:

Enregistrer un commentaire