Accountant م Accountant م - 9 days ago 6
PHP Question

why there are 2 filesize functions in PHP (filesize() and SplFileInfo::getSize)?

I'm sorry this might be ridicules question but I have been using filesize() for my 4 months of php coding (I'm new to php) and I always think of it as the php standard library function for getting a file size. but now I was reading about Standard PHP Library (SPL) and I surprised that there is another function called SplFileInfo::getSize which do exactly the same thing


  1. Is
    filesize()
    considered a standard php function or not ?

  2. If
    filesize()
    considered a standard php function , why the php creators bother themselves by making another function(
    SplFileInfo::getSize
    ) do the same thing?

  3. What is the difference between both functions
    filesize()
    and
    SplFileInfo::getSize
    ? which one should I use?



please note that I'm new to php . thanks

Answer
  1. Is filesize() considered a standard php function or not ?

What is meant by standard? As I see in a manual filesize() exists since php4 (which didn't support OOP in a proper way). So, this function is a procedural way of getting a filesize. You need only path to file and what you get is a size. And nothing else.

  1. If filesize() considered a standard php function , why the php creators bother themselves by making another function SplFileInfo::getSize do the same thing?

In php5 (as you can see getSize is available since php5) more object oriented features had been added to php, they all are part of Standard PHP Library or SPL. So, in php5 you can consider a file like an object with properties - name, size etc. So for getting one property or another - there's a method. For getting filesize is getSize()

So, getSize is not a function, it's a method of a class SplFileInfo.

  1. What is the difference between both functions filesize() and SplFileInfo::getSize()? which one should I use?

As we already learned - filesize() uses a procedural approach:

$filename = '/path/to/file.ext';
$size = filesize($filename);

And as getSize() is a method of a class, object of a class should be created first:

$filename = '/path/to/file.ext';
$finfo_obj = new SplFileInfo($filename);
$size = $finfo_obj->getSize();

So, it's up to you to decide what function and which approach to use.

Comments