I'Ve seen a lot of classes with ONE SIGNLE function in it. Why do they but SINGLE function into class?

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

EDIT:

That $resource is main reason for it being class?

<?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->resize(320, 240);
?>

EDIT2:

what's difference between

<?php
class Image {
    public $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);
?>

Comments

I know that's only avaible in my lass, it doesn't matter right now. But, that's the reason why it's class, right? Because $resource is hidden from outer space

Written by genesis

If the $resource was not private, this could be useful, a straight resize class. As it stands, the $this->resource gets set and then would presumably be unused. Probably needs a getter, or the private is an error.

Written by Orbling

please read EDIT2 and tell me difference

Written by genesis

Where does $resource come from, genesis? It's passed to imagecopyresampled, imagesx, and imagesy before it's ever defined.

Written by webbiedave

the difference in EDIT 2 is this: the class Image is extendible. As a class/object, an Image may have properties/attributes like a width and a height. Maybe the Image is the base for different types of Images like JPEGImage or GIFImage or PNGImage, whose methods like resize or reflect or scale, etc are all treated differently and respective to its type. but maybe they are the same in some ways but different in others. the bottom example is simply a function called resize that does something a single way and makes assumptions about image.

Written by john

added resource. @John: nothing? So finally: is there any serious reason to do it in class?

Written by genesis

@genesis: $resource is still not defined in function resize before it's passed to other functions.

Written by webbiedave

it is already... already before you commented that

Written by genesis

Accepted Answer

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.

Written by Orbling
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki