Then how to begin to design a qualified class, the first write class{} is wrong, the right thing to do is not to write anything but to assume that the class already exists, that the object already exists, that the various attribute methods already exist, and, under this complete assumption, imagine what this object should be used for example, the class where we make a thumbnail , we want to encapsulate it into a class, easy to use next time, we first need to be clear what the object is what it will do, to make thumbnails of the essence of the operation is to shrink the picture and output, here is the operation of the picture, then the object is the picture, because the image on the site is not the only one we have to tell this is This allows you to assume that this class already exists, and at first you have to declare that picture, such as $simg = new Simg ("1.jpg"), and what attributes should a picture have? When making thumbnails, we should be most concerned about the width, height, type, and these three items for a picture is positive, which means that this object must have these attributes, $simg->width, $simg->height, $simg->type, And these properties can be read from the beginning,
Copy Code code as follows:
1 $simg = new Simg ("1.jpg");
2 echo $simg->width;
3 echo $simg->height;
4 echo $simg->type;
5//This object, should be able to operate this way.
According to the principle of OOP, if the object's properties are changed, the object should change accordingly, which means that we can assign it a value, get the width of the object, the height, and after the calculation (such as scaling down), re-assign the value back. Our essence is to create a thumbnail image of a picture, that is, to create a new picture, after changing it, the next thing to do is to save the changed picture, save it is a process, so it will be a method. For example $simg->save (), taking into account the need to change a place to save. At least change a name, that is to say, when used, this object should be described, the picture saved to ... This means that the method has a parameter, which is where to save it.
Copy Code code as follows:
$simg = new Simg ("1.jpg");//instantiation
$simg->width = 200;//Set width
$simg->height = 200;//Set height
$simg->save ("2.jpg");//Save to 2.jpg
When using this class, the thought description and the written code should be exactly the same. There is a little problem with the thinking here that may produce misleading ideas that do not conform to the principles of OOP, which does not conform to the object-oriented: the size of the object's property is not changed, and the change is saved. In other words, this object is actually a copy of the source object in PHP memory, we changed the size of the replica and saved, so the picture was really changed before the attributes of the picture should be read-only, rewriting is invalid, so if the original image as the object to describe. This description should be more accurate: The picture changes size to save as. And the size of the original is not changed, the size of the change is a process, which means that this is also a method,
Copy Code code as follows:
This class is supposed to work like this.
Instantiate a picture
$simg = new Simg ("1.jpg");
Read picture width height calculation ratio
$simg->width
$simg->height
The picture uses the specified width to save as ...
$simg->size (200,200)->save ("2.jpg");
This is described as an object of the original image, although it is a nonexistent class, but its usage must exist in advance and conform to the principle of OOP, which is what it is and what it can do. If you think about it from a different point of view, as an object to the image that is about to be exported, then it should be empty when it is created, and then it must be based on a piece of artwork, then resize it, and then save it.
Copy Code code as follows:
Describe it as a way of thinking. The code is supposed to look like this.
$simg = new Simg (); It was empty at first.
Echo $simg->width; It must be 0.
$simg->load ("1.jpg"); Base on a graph
Echo $simg->width; No, it's the original size.
Change size
$simg->width = 200;
$simg->height = 200;
$simg->save ("2.jpg"); Save it.
It doesn't seem so obvious at this point.
The following will be better:
Copy Code code as follows:
$simg = new Simg ("2.jpg"); First is empty, specify a filename
$simg->load ("1.jpg"); Base on a graph
Change size
$simg->width = 200;
$simg->height = 200;
$simg->save (); Save it.
This is more obvious, instantiating a thumbnail, but it does not exist until it is saved before it exists on the hard disk.
Here we first create this class in the first way, based on the original object, according to the analysis as follows:
Copy Code code as follows:
? Php
Class Simg {
Public $width = 0;
Public $height = 0;
Public function __construct ($img) {
}
Public function Size ($width, $height) {
}
Public function Save ($path) {
}
}
Then, based on the requirements for each of the properties of each method, fill in the code, the first must know the height of the file, width, because the PHP processing different types of pictures using different functions, we have to know what the file type is. To decide which function to use to design the class, is to think about "how to do" time, to the instantiation, immediately know the width of the height. Must be done in the constructor, only the constructor executes when the class is instantiated, where we can use the getimagesize function to get the width, height, type, width and height of the file, where we can assign it to the property. This way, the instantiation of the picture, the problem of getting the property is solved, the process of changing the size?
Copy Code code as follows:
? Php
Class Simg {
Public $width = 0;
Public $height = 0;
Public function __construct ($img) {
$var = getimagesize ($img);
$this->width = $var [0];
$this->height = $var [1];
}
Public function Size ($width, $height) {
}
Public function Save ($path) {
}
}
Since the commonly used picture types on the network are GIF jpg png three, the other types are temporarily not considered, the sizing method. Nothing is done before the output. It can be said that our code, as long as we know to output the picture is how large it can be but, different methods, internal variables are not universal. How to register a global variable is easy to interfere with external variables and pollution, then we use the properties of the class to save. New plus two attributes are tentatively here W and H, these two properties are strictly not attributes, but we use attributes to pass variables between methods, in order to avoid them being accessed and modified outside of the class we use the keyword private to access restrictions when defining, private $w = 0;private $h = 0;
Copy Code code as follows:
Public function Size ($width, $height) {
$this->w = $width;
$this->h = $height;
}
To change the size of the method, as long as the output to the width and height to write down on it. The following is saved, to save before, we have to make the picture smaller before the line, so, the thumbnail calculation process, mainly completed here, you need to load the original image to be reduced, but also to know the file type, because different types of picture loading way is different, file name and file type. Before the constructor is known, we add two public properties at this time,
Copy Code code as follows:
Public $width = 0;
Public $height = 0;
public $path = ';
Public $type = 0;
Private $w = 0;
Private $h = 0;
Public function __construct ($img) {
$var = getimagesize ($img);
$this->width = $var [0];
$this->height = $var [1];
$this->path = $img;
$this->type = $var [2];
}
After that, we can load the original image in the saved method, change the size, and save it to the specified location, and as a function of the different type calls of the Save method, you can choose to use switch ($var [2]) to make a new thumbnail and then save it.
Copy Code code as follows:
By how this class is written ... This should be the way to use
$simg = new Simg ("1.jpg");
Read width high and compute
$simg->width
Set size
$simg->size (200, 200);
Saved to
$simg->save ("2.jpg");
And the description is a bit different, because the description is: use (this) size, save As (here) this description is a bit of a detour, if you can write this is ok $simg->size ("2.jpg")->save; object usage must be such an object- > method () This requires that the value in the preceding amount must be an object, save before size This requires that the return value of size must be an object, but this method has nothing to return, and this object must be the current object before there is a Save method. No object doesn't matter we add one ourselves,
Copy Code code as follows:
Public function Size ($width, $height) {
$this->w = $width;
$this->h = $height;
return $this;
}
Returns the current object so that you can use the image (this) size, save As (here) $simg->size->save ("2.jpg"); Such an OOP-compliant class encapsulation is complete.