PHP Object-oriented tutorial for custom class _php instances

Source: Internet
Author: User
Tags benchmark types of functions
How to start to design a qualified class, the first to write class{} is wrong, the right is nothing to write, but assume that the class already exists, this object already exists, various attribute methods have already, under this completely hypothesis imagine this object should use, for example, we make a thumbnail of the class , we want to encapsulate into a class, to facilitate the next use, we first need to clarify what the object is what it will do, to make thumbnails of the essence of the operation is to reduce the image and output, here is manipulated by the picture, then the object is the picture, because the site is not the only picture, we have to tell this is the picture, This can be assumed that the next class already exists, the first to declare is the picture, such as $simg = new Simg ("1.jpg"); So, what are the properties of a picture? In the production of thumbnails, we should be most concerned about the width, height, type, and these three items for a picture is positive, which means that this object must have these properties, $simg->width, $simg->height, $simg->type, And these properties can be read from the beginning,

Copy the Code code 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, if the object's properties are changed, the object should also change accordingly, which means that we can assign a value to it, get the object's width, height, calculate (for example, proportionally) and re-assign the value back. Our essence is to make a thumbnail image, that is, to create a new picture, after changing it, the next thing to do is to save the changed picture, stored up is a process, so it will be a method. For example $simg->save (), considering the need to change a place to deposit. At least change a name, that is, in use, this object should be described as such, the picture is saved to ... This means that this method has one parameter, which is where to save.

Copy the Code code as follows:
$simg = new Simg ("1.jpg");//instantiation
$simg->width = 200;//Set width
$simg->height = 200;//setting height
$simg->save ("2.jpg");//Save to 2.jpg

When using this class, the thought description and written code should be exactly the same, there is a small problem in the thinking description, there may be inconsistent with the OOP thinking principle of the misleading, this does not conform to the object-oriented is: object property re-assignment of the size of the original image is not changed, the change is saved, In other words, this object is actually a copy of the source object in PHP memory, we changed the size of the copy and saved it, so the picture is really changed before the properties of the picture should be read-only, rewrite is invalid, so, if the original image as an object to describe. The description should be more accurate: The picture changes size and then saves as. The size of the original is not changed, and the change in size is a process, which means that this is also a method,

Copy the Code code as follows:
This class, it should be used.
Instantiate a picture
$simg = new Simg ("1.jpg");
Read picture width height calculation scale
$simg->width
$simg->height
The picture is saved with the specified width and height as ...
$simg->size (200,200)->save ("2.jpg");

This is described in terms of the original image as an object, though it is a nonexistent class, but its usage must exist beforehand and conform to the thought principle of OOP, which is what it is and what it can do. If you think from a different angle and take the image that is about to be exported as an object, then it should be empty when the object is created, then it must be based on an original image, then resize it and save it.

Copy the Code code as follows:
Describe this idea. The code should look like this.

$simg = new Simg (); It was empty at first.
Echo $simg->width; Must be 0.
$simg->load ("1.jpg"); Take a picture as a benchmark
Echo $simg->width; No, it's the original size.

Change size
$simg->width = 200;
$simg->height = 200;
$simg->save ("2.jpg"); Save it.

It doesn't seem so obvious at this point.

The following will be better:

Copy the Code code as follows:
$simg = new Simg ("2.jpg"); is empty at first, specify a file name
$simg->load ("1.jpg"); Take a picture as a benchmark
Change size
$simg->width = 200;
$simg->height = 200;
$simg->save (); Save it.

This is more obvious, instantiating a thumbnail, but it does not exist until it is saved in the hard disk.

Here we first create this class in the first way, based on the angle of the original object, according to the analysis below:

Copy the Code code as follows:
Class Simg {
Public $width = 0;
Public $height = 0;
Public function __construct ($img) {
}
Public function Size ($width, $height) {
}
Public function Save ($path) {
}
}

Then, according to each method of each property requirements, fill in the code, the beginning must know the height of the file, width, because PHP processing different types of images using different functions, we have to know the file type here. To decide which function to use to design the class, it is time to think about how to do it, and to know the width and height immediately after the instantiation. Must be done in the constructor, only the constructor will be executed when the class is instantiated, and here we can use the getimagesize function to get the width, height, type, width and height of the file, which we can assign to the property here. In this way, to instantiate the picture, the problem of getting the property is solved, the process of changing the size?

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


Due to the types of images commonly used on the web there are gif jpg png three, other types are temporarily not considered, resizing methods. There is nothing to do before the output. It can be said that our code, as long as we know the output of the picture is how big it can be but, different methods, internal variables are not universal. How to register a global variable that is susceptible to interference and contamination by external variables, we use the properties of the class to save. The new plus two properties are tentatively W and H, these two properties, strictly not attributes, but we use attributes to pass variables between methods, in order to avoid them being accessed and modified outside of the class we use the keyword private to restrict access when defining, private $w = 0;private $h = 0;

Copy the Code code as follows:
Public function Size ($width, $height) {
$this->w = $width;
$this->h = $height;
}

Change the size of the method, as long as the width and height of the output to be written down on it. The following is saved, to save, you have to make the picture smaller before the line, so, the calculation process of the thumbnail, mainly completed here, need to load the original image to reduce, but also to know the file type only line, because different types of pictures loaded in a different way, file name and file type. In the constructor it is known that at this point we add two more public properties,

Copy the Code code 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 in the saved method, change the size, and save it to the specified location, as the method of saving the different types of functions called the function when different can choose to use switch ($var [2]) to judge in a new thumbnail and then save.

Copy the Code code as follows:
By the way this class is written: The way it should be used

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

Read wide and calculate
$simg->width

Set size
$simg->size (200, 200);
Saved to
$simg->save ("2.jpg");

And the description is a bit different, because the description is: use (this) size, save As (here) this description is a bit around the mouth, if you can write this is no problem $simg->size (->save) ("2.jpg"); object usage must be such an object- > method () This requires that the value in the preceding quantity must be an object, save is preceded by size which requires that the return value of size must be an object, but this method has nothing to return, and this object is definitely the current object, there will be a Save method, No object does not matter we add one ourselves,

Copy the Code code as follows:
Public function Size ($width, $height) {
$this->w = $width;
$this->h = $height;
return $this;
}

Returns the current object so that the image can be used (this) size, saved as (here) $simg->size (->save) ("2.jpg"), a class package that conforms to the OOP idea is complete.

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