Home >Backend Development >PHP Tutorial >PHP object-oriented tutorial custom class_PHP tutorial
Then how to start designing a qualified class? It is wrong to write class{} at the beginning. The correct thing is to write nothing. Instead, assume that this class already exists, this object already exists, and various attribute methods We already have it. Under this complete assumption, imagine how this object should be used. For example, if we make a thumbnail class, we hope to encapsulate it into a class for easy use next time. We first need to clarify what the object is and what it will do. What, the essential operation of making a thumbnail is to reduce the image and output it. The image being manipulated here is the image, so the object is the image. Since the image on the website is not the only one, we have to tell which image it is, so we can assume this class It already exists. You must declare that picture at the beginning, for example $simg = new simg("1.jpg"); So, what attributes should a picture have? When making thumbnails, what we are most concerned about should be width, height, and type, and these three items are certain for a picture, which means that this object must have these attributes, $simg->width, $simg->height, $simg->type, and these attributes can be read from the beginning,
According to the principle of oop's thinking, if the properties of the object are changed, the object should also change accordingly, which means that we can assign a value to it, obtain the width and height of the object, and after calculation (such as scaling down ), reassign it back. Our essence is to make a thumbnail of a picture, that is, to generate a new picture. After changing it, the next thing to do is to save the changed picture. Saving is a process. So it would be a method. For example, $simg->save(), consider saving in another place. At least one name needs to be changed, that is to say, when used, the object should be described like this, the picture is saved to... This means that this method has a parameter, which is where to save it.
When using this class, the thinking description and the written code should be completely consistent. There is a small problem in the thinking description here, which may lead to misdirection that is not in line with the oop thinking principle. What is not in line with object-oriented here is: object Why did the size of the original image not change when the attribute was reassigned? What changed was that it was saved separately, which means that this object is actually a copy of the source object in the PHP memory. We changed the size of the copy and saved it, so the image was The attributes of the image before it is actually changed should be read-only, and rewriting is invalid. Therefore, if the original image is used as an object to describe it. This description should be more accurate: the image is resized and saved as . The size of the original image has not changed. Changing the size is a process, which means this is also a method,
This is described from the perspective of the original image as an object. Although it is a class that does not exist, its usage must exist in advance and conform to the ideological principles of oop, that is, what is this thing and what can it do? What. If we think about it from another perspective, taking the image to be output as the object, then this object should be empty when it is created, and then it must be based on a certain original image, then adjust its size, and then Save it,
$simg = new simg(); //It is empty at first
echo $simg->width; //It must be 0
$simg->load("1.jpg") ; //Based on a picture
echo $simg->width; //It has not been changed, it is the original size
//Change size
$simg->width = 200;
$simg->height = 200;
$simg->save("2.jpg"); // Save it
It doesn’t seem obvious at this point
The following will be better:
This will be more obvious, instantiating a thumbnail, but it does not exist yet, it will not exist on the hard drive until after saving.
Here we first create this class from the perspective of the original image as the object according to the first method. Based on the above analysis, we are as follows:
Then fill in the code inside according to the requirements for each attribute of each method. You must know the height and width of the file at the beginning. Since PHP uses different functions to process different types of images, we have to here Know what the file type is. When designing a class to decide which function to use, it is time to think about "how to do it". You must know the width and height immediately after instantiation. It must be done in the constructor. Only the constructor will be executed when the class is instantiated. Here we can use the getimagesize function to obtain the width, height, type, width and height of the file. We can assign values to the attributes here. In this way, the problem of instantiating the image and getting the attributes is solved. What about the process of changing the size?
To change the size, just temporarily write down the width and height to be output. The following is saving. Before saving, you must first reduce the image size. Therefore, the thumbnail calculation process is mainly completed here. You need to load the original image to reduce it. Moreover, you must also know the file type. Because different types of images have different loading methods, file names and file types. We only know this in the constructor. At this time we add two more public properties,
After that, we can load the original image, change the size, and save it to the specified location in the save method. As for the save method, when writing different types of functions to call, you can choose to use switch ($var [2]) Make a judgment before creating a new thumbnail and save it.
$simg = new simg("1.jpg");
//Read the width and height and calculate
$simg->width
//Set size
$simg->size(200, 200);
//Save to
$simg->save("2.jpg");
is a bit different from the description, because the description is: use (this) size, save as (here). This description is a bit convoluted. If you can write it like this, it will be fine. $simg->size(200, 200)- >save("2.jpg"); The use of the object must be such an object->Method (), which requires that the value in the previous quantity must be an object. The front of save is size, which requires the return value of size. It must be an object, but this method has nothing to return, and this object must be the current object, so there will be a save method. If there is no object, it doesn’t matter if we add one ourselves,
www.bkjia.com