PHP Object-Oriented custom class _php tutorial

Source: Internet
Author: User
Tags types of functions
  the so-called object-oriented is when what what things do, we design the class when we need to think about how to do the content, then what kind of a class can be considered as the idea of OOP, the answer is: This class after writing, in the process of use, Can accurately represent a thing, in writing the code should be consistent with the thinking, that is, what this thing does. 
then how 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 how to use, For example, we make a thumbnail class, we want to encapsulate into a class, for the next use, we need to clarify what the object is what it will do, to make the thumbnail operation is to reduce the image and output, here is manipulated by the picture, then the object is the picture, Because the picture on the website is not unique, we have to tell this is the picture, this can be assumed that the next class already exists, the first thing to declare is that picture, such as $simg = new Simg ("1.jpg"); What attributes should a picture have? When making thumbnails, we should be most concerned with width, height, type, and these three items are positive for a picture, which means that this 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 do so. 

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. 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. 
$simg New simg ("1.jpg"); // instantiation of $simg->width = 200; // Set Width $simg->height = 200; // Set 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 of the 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, that is, 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 property of the picture should be read-only, rewrite is invalid,  so, If you use the original 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,
// This class, it should be used. Instantiate a picture $simgnew simg ("1.jpg"); // Read picture width height calculation scale  $simg-$simg,height// picture Use the specified wide-height save 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.
// describe this idea. The code should look like this $simgnew simg ();  // it was empty at first. Echo $simg->width;   // must be 0 . $simg // take a picture as a benchmark Echo $simg->width;    // no change, it's the original size//size $simg->width =; $simg->height =$simg->save ("2.jpg");  // save it.
It doesn't seem so obvious at this point.
The following will be better:
$simg New simg ("2.jpg");  // is empty at first, specify a file name $simg // Base//Change the size of a picture $simg->width =; $simg->height =$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:

 
  PHPclass  simg {    public$width = 0;      Public $height = 0;          Public function __construct ($img) {            }    publicfunction Size ($width$height) {        }    publicfunction 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 pictures using different functions, we have to know what the file type is 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?
 
  PHPclass  simg {    public$width = 0;      Public $height = 0;          Public function __construct ($img) {        $vargetimagesize($ IMG);         $this $var [0];         $this $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 access restrictions when defining,private $w = 0;private  $h = 0;
   Public function size ($width$height) {        $this$width;         $this$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,
  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.
// by the way this class is written: The method of use should be this $simgnew simg ("1.jpg"); // read wide and calculate $simgwidth// set size $simg->size (.); // 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 must be used as an object--and method () This requires that the value in the preceding amount must be an object, and save is preceded by size This requires that the return value of size must be an object, but there is nothing in this method that needs to be returned, and this object is definitely the current object. There will be a Save method, no object does not matter we add one ourselves,
 Public function size ($width$height) {        $this$width;         $this $height ;         return $this ;    }

returns the current object so that you can use the The picture uses (this) size, save As (here) the $simg->size ((here)->save ("2.jpg"); Such a class package that conforms to OOP thought is done. 


http://www.bkjia.com/PHPjc/781158.html www.bkjia.com true http://www.bkjia.com/PHPjc/781158.html techarticle The so-called object-oriented is when what is what to do, we design the class when we need to think about how to do the content, then what kind of a class is in line with the idea of oop, ...

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