Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize classes in PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to customize classes in PHP. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

So how to start to design a qualified class? it is wrong to write class {} at the beginning, the right thing is to write nothing, but to assume that the class already exists, the object already exists, and all kinds of properties and methods already exist. Imagine how this object should be used under this complete assumption. For example, if we make a class with a thumbnail, we want to encapsulate it into a class to facilitate our next use. First of all, we need to make clear what the object is and what it will do. To make thumbnails, the essential operation is to zoom out the picture and output it. What is operated here is the picture, then the object is the picture. Since the picture on the website is not the only one, we have to tell the picture that this is the picture, so we can assume that the class already exists and declare that it is the picture at the very beginning, for example, $simg = new simg ("1.jpg"). So, what attributes should a picture have? When making thumbnails, we should be most concerned about width, height, and type, and these three items are certain for a picture, which means that the object must have these properties, $simg- > width,$simg- > height,$simg- > type, and these properties can be read from the beginning.

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 property of the object is changed, the object should also be changed accordingly, which means that we can assign a value to it, get the width and height of the object, calculate it (for example, scale it down), and re-assign it back. Our essence is to make a thumbnail of a picture, that is, to generate a new picture, and after changing it, the next thing to do is to save the changed picture, which is a process. So it's going to be a way. For example, $simg- > save (), considering that you want to store it in a different place. At least one name should be changed, that is to say, when in use, the object should be described like this, and the picture should be saved to. This means that this method has one parameter, which is where to save it.

$simg = new simg ("1.jpg"); / / instantiate

$simg- > width = 200 ram / set width

$simg- > height = 200 ram / set height

$simg- > save ("2.jpg"); / / Save to 2.jpg

When using this class, the thinking description and the written code should be exactly the same. There is a small problem with the thinking description here, which may lead to misleading that does not conform to the oop thinking principle. What is not in line with the object-oriented is why the size of the original image has not changed, and the change is saved, that is to say, 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 properties of the picture should be read-only before the picture is really changed, and rewriting is invalid, so if you use the original image as the object to describe. This description should be more accurate: the picture is resized and saved as. The size of the original image has not changed, and changing the size is a process, which means that it is also a method.

/ / this class should be used like this.

/ / instantiate a picture

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

/ / calculate the ratio of reading the width and height of the picture

$simg- > width

$simg- > height

/ / the picture is saved as using the specified width and height.

$simg- > size (200200)-> save ("2.jpg")

This is described from the point of view of the original image as the object, although it does not exist as a class, but its usage must exist in advance, and in line with the principle of oop, that is, what it is and what it can do. If you think about it from another point of view, taking the picture to be output as the object, then when the object is created, it should be empty, and then it must be based on a certain original image, and then resize it. And save it.

/ / describe it according to this train of thought. The code should look like this.

$simg = new simg (); / / empty at first

Echo $simg- > width; / / definitely 0

$simg- > load ("1.jpg"); / / based on a picture

Echo $simg- > width; / / unchanged, it is the original image size

/ / change the size

$simg- > width = 200

$simg- > height = 200

$simg- > save ("2.jpg"); / / Save it

It doesn't seem obvious at this time.

The following would be better:

$simg = new simg ("2.jpg"); / / initially 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 it

This makes it more obvious to instantiate a thumbnail, but it doesn't exist yet, and it doesn't exist on the hard drive until it is saved.

Here, we first create this class from the point of view of the original image in the first way, according to the above analysis:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report