PHP object-oriented tutorial-custom class

Source: Internet
Author: User

So how can we start designing a qualified class? At the beginning, I wrote all the errors of class {}. The correct one is that I did not write anything, but assumed that this class already exists, this object already exists and various attribute methods are available. Under this complete assumption, imagine how to use this object. For example, we create a thumbnail class, we want to encapsulate the image into a class for future use. First, we need to specify what the object is and what it will do. The essential operation to create a thumbnail is to narrow down the image and output it, the image is operated here, so the object is the image. Because the image on the website is not the only one, we have to tell this image. This can be assumed that the class already exists, at the beginning, you must declare the image. For example, $ simg = new simg ("1.jpg"); what attributes should an image have? When creating a thumbnail, we are most concerned with width, height, and type. These three items are positive for an image, which means that this object must have these attributes, $ simg-> width, $ simg-> height, $ simg-> type, which can be read from the beginning,

Copy codeThe Code is 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 be operated in this way.

According to the principles of oop, if an object's attributes are changed, the object should also change accordingly. This means that we can assign values to it to obtain the width and height of the object, after calculation (for example, scale down proportionally), assign a value again. In essence, we want to create a thumbnail of an image, that is, to generate a new image. After we change it, the next thing we need to do is to save the changed image, it is a process, so it will be a method. For example, $ simg-> save () must be stored in another place. At least change the name. That is to say, this object should be described in this way. Save the image ...... This means that there is a parameter in this method, that is, where to save it.

Copy codeThe Code is as follows:
$ Simg = new simg ("1.jpg"); // instantiate
$ Simg-> width = 200; // set the width.
$ Simg-> height = 200; // set the height.
$ Simg-> save ("2.jpg"); // save to 2.jpg

When using this class, the thought description should be exactly the same as the written code. Here, the thought description has a small problem and may lead to misleading ideas that do not conform to the oop thinking principles, what does not conform to the object-oriented principle is: Why does the source image size remain unchanged after the object attribute is re-assigned? What changes are stored in another way, that is to say, this object is actually a replica of the source object in the php memory, we changed the size of the replica and saved it. Therefore, the attributes of the original image should be read-only, and the rewrite is invalid, if the source image is used as an object. This description should be more accurate: change the image size and save it. The size of the source image is not changed, and the size change is a process, which means this is also a method,

Copy codeThe Code is as follows:
// This class should be used like this.
// Instantiate an image
$ Simg = new simg ("1.jpg ");
// Calculate the ratio of read image width to height
$ Simg-> width
$ Simg-> height
// Save the image with the specified width and height ......
$ Simg> size (200,200)-> save ("2.jpg ");

 

This is described from the perspective of the source image as an object. Although it is a class that does not exist, its usage must exist beforehand and comply with the idea principle of oop, that is, what is this, what can it do. From another point of view, if the output image is used as an object, the object should be empty when it is created, then it must take an original image as the benchmark, adjust its size, and save it,

Copy codeThe Code is as follows:
// Follow the instructions below. The code looks like this.

$ Simg = new simg (); // It is empty at the beginning.
Echo $ simg-> width; // It must be 0
$ Simg-> load ("1.jpg"); // use a picture as the reference
Echo $ simg-> width; // The Source image size has not been changed.

// Change the size
$ Simg-> width = 200;
$ Simg-> height = 200;
$ Simg-> save ("2.jpg"); // save it

It does not seem obvious at this time.

The following will be better:

Copy codeThe Code is as follows:
$ Simg = new simg ("2.jpg"); // It is empty at the beginning and specifies a file name.
$ Simg-> load ("1.jpg"); // use a picture as the reference
// Change the size
$ Simg-> width = 200;
$ Simg-> height = 200;
$ Simg-> save (); // save it

This will be more obvious. a thumbnail is instantiated, but it does not exist until it is saved on the hard disk.

Here, we first create this class from the perspective of the source image as an object in the first method, according to the above analysis:

Copy codeThe Code is 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 attribute of each method, fill in the code in. At first, you must know the file height and width. Because php uses different functions to process different types of images, we have to know the file type here. To decide which function design class to use, you need to know the width and height immediately after instantiating the function design class. It must be done in the constructor. Only the constructor will execute it during class instantiation. here we can use the getimagesize function to obtain the width, height, type, width, and height of the file, here, we can assign values to attributes. In this way, the attribute problem is solved when the image is instantiated. What about the process of changing the size?

Copy codeThe Code is 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 ){
}
}


Because there are three common types of images on the Network: gif and jpg. png, other types are not considered for the time being. Do nothing before output. It can be said that our code only needs to know the size of the image to be output. However, internal variables are not universal in different methods. What should we do to register global variables that are easily disturbed and contaminated by external variables? Then we can use the attributes of the class to save them. The newly added attributes are tentatively set to w and h. These two attributes are strictly not attributes, but attributes are used to pass variables between methods, to prevent them from being accessed and modified outside the class, we use the private keyword to restrict the access when defining them. private $ w = 0; private $ h = 0;

Copy codeThe Code is as follows:
Public function size ($ width, $ height ){
$ This-> w = $ width;
$ This-> h = $ height;
}

To change the size, you only need to temporarily write down the width and height of the output. The following figure shows how to save the thumbnail. Before saving the thumbnail, you must make the image smaller. Therefore, the thumbnail computation process is mainly completed here. You need to load the source image to narrow down the thumbnail, you also need to know the file type, because different types of images are loaded in different ways, file names and file types. Only when the constructor knows, now we add two public attributes,

Copy codeThe Code is 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];
}

Then, we can load the source image, change the size, and save it to the specified position in the SAVE method, you can use switch ($ var [2]) to create a new thumbnail and save it when writing different types of called functions.

Copy codeThe Code is as follows:
// Follow the Writing Method of this class .. The method should be as follows:

$ Simg = new simg ("1.jpg ");

// Read width and height and calculate
$ Simg-> width

// Set the size
$ Simg-> size (200,200 );
// Save
$ Simg-> save ("2.jpg ");

It is a little different from the description, because the description is: Use (this) size, save as (here) This description is a bit of a detour, if you can write it like this, it will be okay $ simg-> size (200,200) -> save ("2.jpg"); the use of an object must be such an object-> method (). This requires that the value in the preceding amount must be an object, before saving, there is a size. This requires that the returned value of size must be an object, but there is nothing to return in this method, and this object must be the current object, so there will be a save method, if there are no objects, it doesn't matter if we add one ourselves,

 Copy codeThe Code is 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 (this) size of the image and save it as (here) $ simg-> size (200,200)-> save ("2.jpg "); this class is encapsulated in line with the oop idea.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.