PHP object-oriented tutorial-custom class

Source: Internet
Author: User
The so-called object-oriented is when and what to do, and what we need to think about when designing a class is what to do. So what kind of class is in line with the idea of OOP? The answer is: after this class is written

The so-called object-oriented is when and what to do, and what we need to think about when designing a class is what to do. So what kind of class is in line with the idea of OOP? The answer is: after this class is written

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,

The 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 this method has a parameter, that is, where to save it.

The 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,

The 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,

The 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:

The 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:

The Code is as follows:


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?

The Code is as follows:


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;

The Code is as follows:

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.