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, it can accurately represent a thing in the process of use. how can we design a qualified class in the book, at the beginning, I wrote all the class {} Errors. the correct one is that I did not write anything. Instead, I assumed that this class already exists, this object already exists, and various attribute methods already exist, imagine how to use this object. for example, if we create a thumbnail class, we want to encapsulate it into a class for future use, first, we need to clarify what the object is and what it will do. The essential operation for creating a thumbnail is to narrow down the image and output it. here the image is operated, and the object is an image, because the pictures on the website are not the only one, we have to tell this picture. this can be assumed that this category already exists. at the beginning, we have to declare it as the image, for example, $ simg = new simg ("1.jpg"); what are the attributes of an image? 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 there is a parameter in this method, 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:
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,

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

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

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