Home  >  Article  >  Backend Development  >  PHP object-oriented tutorial custom class_PHP tutorial

PHP object-oriented tutorial custom class_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:37675browse

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,

Copy code 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 operate like this.

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.

Copy code The code is 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 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,

Copy code The code is as follows:

//This class should be used like this.
//Instantiate a picture
$simg = new simg("1.jpg");
//Read the width and height of the picture to calculate the ratio
$simg->width
$simg->height
//Save the image as...
$simg->size(200,200)->save("2.jpg");

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,

Copy code The code is as follows:

//Describe in this way. The code should look like this

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

Copy code The code is as follows:

$simg = new simg("2.jpg"); //The beginning is Empty, specify a file name
$simg->load("1.jpg"); //Based on a picture
//Change the size
$simg->width = 200 ;
$simg->height = 200;
$simg->save(); //Save

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:

Copy code 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 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?

Copy code 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) {
}
}


Since there are three commonly used image types on the Internet: gif jpg png, other types will not be considered for the time being. How to adjust the size. Nothing is done before output. It can be said that our code only needs to know the size of the image to be output. However, different methods and internal variables are not universal. What to do? Registered global variables are easily disturbed and polluted by external variables, so we use class attributes to save them. The two newly added attributes are tentatively named w and h. These two attributes are not strictly speaking attributes. We just use attributes to transfer variables between methods. In order to prevent them from being accessed and modified outside the class, we add When defining, use the keyword private to restrict access, private $w = 0;private $h = 0;

Copy code The code is as follows:

public function size($width, $height) {
$this-> ;w = $width;
$this->h = $height;
}

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,

Copy code 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];
}

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.

Copy code The code is as follows:

//Follow the way this class is written. . How to use it should be like this

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

Copy code The code is as follows:

public function size($width, $height) {
$this-> ;w = $width;
           $this->h = $height; 
                                                          
Return the current object, so you can use (this) size as a picture and save it as (here) $simg->size(200, 200)->save("2.jpg"); such a match The class encapsulation of oop ideas is completed.

http://www.bkjia.com/PHPjc/781411.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/781411.htmlTechArticleThen how to start designing a qualified class? It’s wrong to write class{} from the beginning. It’s correct. Write nothing, but assume that this class already exists, this object already exists, and various attributes...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn