Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there reason to create class for single function?

I've seen a lot of classes with a SINGLE function in it. Why do they put a SINGLE function into class?

I use classes just to make things more clear, but about those who put a SINGLE function into class? Is there any reason for it?

I see no difference between these:

<?php
class Image {
    private $resource;
    function resize($width, $height) {
        $resized = imagecreatetruecolor($width, $height);
        imagecopyresampled($resized, $this->resource, 0, 0, 0, 0, $width, $height, imagesx($this->resource), imagesy($this->resource));
        $this->resource = $resized;
    }
}
$image = new Image();
$image->resource = "./someimage.jpg";
$image->resize(320, 240);

and

    function resize($width, $height) {
        $resource = "./someimage.jpg";    
        $resized = imagecreatetruecolor($width, $height);
        imagecopyresampled($resized, $resource, 0, 0, 0, 0, $width, $height, imagesx($resource), imagesy($resource));
        $resource = $resized;
        return $resource;
    }

resize(320, 240);

My thought was that $resource is the main reason, because it's private:

class Image {
    private $resource;
    function resize($width, $height) {
        $resized = imagecreatetruecolor($width, $height);
        imagecopyresampled($resized, $this->resource, 0, 0, 0, 0, $width, $height, imagesx($this->resource), imagesy($this->resource));
        $this->resource = $resized;
    }
}

$image->resize(320, 240);

and therefore isn't accessible to the global scope. But why isn't a simple function used in this case?

like image 722
genesis Avatar asked Jun 20 '11 21:06

genesis


People also ask

Why do we use class instead of function?

Classes group methods (functions) AND data together, based on the concept of encapsulation. For lager projects it often becomes easier to group things this way. Many people find it easier to conceptualizes the problem with objects. The point of OOP is not to group functions.

When should a function be a class?

You should use classes only if you have more than 1 function to it and if keep a internal state (with attributes) has sense. Otherwise, if you want to regroup functions, just create a module in a new .

Should I make a class or function?

In general, a function needs well defined inputs and outputs. Classes, in essence, collect functions and data together. Much like you should use a function to collect operations into concise, well defined collections of operations, classes should organize functions and data relevant to be stored together.

What is the purpose of class function?

A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.


2 Answers

Classes are not just "function containers", they are there to represent an object, an entity. They are supposed to encapsulate the data required for the given entity with methods that work for it.

Sometimes there might be a class of object that only needs one method defined for it, but nevertheless it only belongs to that class of object.

like image 107
Orbling Avatar answered Oct 06 '22 09:10

Orbling


I mostly do embedded programming, and seldom use classes. But a single function class could possibly be used to -

  • inherited later
  • enforce integrity of the data structure that is private to the class (encapsulation).
  • may be used to maintain the homogeneity of the code.
like image 36
Lord Loh. Avatar answered Oct 06 '22 08:10

Lord Loh.