Why should I use File::copy, if PHP already has copy?

Asked

Viewed 232 times

2

I am using the Laravel in various projects that I use.

I needed to copy a particular file from one location to another, and I saw in the Laravel documentation that I should use File::copy().

Out of curiosity, as I always do, I decided to take a look at the source code, to know how this method was built and surprised me.

The File class is a facade for Illuminate\Filesystem\Filesystem, which is the "royal class".

See the source code of the method File::copy()

     /**
     * Copy a file to a new location.
     *
     * @param  string  $path
     * @param  string  $target
     * @return bool
     */
    public function copy($path, $target)
    {
        return copy($path, $target);
    }

It makes it seem that File::copy is a facade for native function copy.

So what are the reasons for using the copy method, since it is simply a return of the function copy of PHP?

Should I use File::copy only to maintain the standard encoding framework?

1 answer

3


The idea of frameworks is to create a high-level layer to facilitate the use of some language resources, separating their code from a specific implementation thus resulting in a low coupling of their logic to the implementation, favoring reuse.

In this specific case the implementation is running a native function, but we can replace this class with another implementation without changing its original code.

A great example is class Filesystem of Laravel himself, who in the version 5.0 has been replaced by an external component which now supports, in addition to local files, files on cloud providers such as Amazon S3. All this without having to change a line of code of our application.

  • 1

    Ah, then you noticed a positive point that I had imagined :). Because if, within the class, they use $this->copy, when you need to change this method, all changes will be applied... reuse :>

  • 1

    there is also the fact that native functions go into disuse or even change names..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.