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?
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.
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 .
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.
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.
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.
I mostly do embedded programming, and seldom use classes. But a single function class could possibly be used to -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With