Example code of using PHP classes

Source: Internet
Author: User
User-defined classes are also essential for learning PHP well. Compared with other object-oriented languages, PHP classes are quite simple. PHP only includes class, method, attributes, and extensions. If you are not familiar with using object-oriented languages such as C ++, Java, and Delphi to develop programs, read the book on object-oriented concepts first. I believe this will bring a lot of gains.
The following example is the trolley type. We can see that the class indicates that it is a class. A function in a category. for example, add_item indicates a method of the class. Methods can encapsulate the actual processing situation of the class, so that the class can perform some steps according to the encapsulated method.

The $ this class variable in the program is also the same as the $ GLOBALS and $ php_errormsg variables. it is a special variable in PHP. $ This variable only uses the class to represent the class itself.
The code is as follows:
// Program name: cart. inc
Class Cart {
Var $ items; // trolley class

// Add $ num items to the trolley (add $ artnr variable) in this method)
Function add_item ($ artnr, $ num ){
$ This-> items [$ artnr] + = $ num;
}

// This method reduces $ num items from the trolley (from the $ artnr variable)
Function remove_item ($ artnr, $ num ){
If ($ this-> items [$ artnr]> $ num ){
$ This-> items [$ artnr]-= $ num;
Return true;
} Else {
Return false;
}
}
}
?>


To use a trolley, you can use a method similar to the following example. You can first save each class as an Include file, and then add it to require or include. When defining the variable $ cart, use the reserved word of new to indicate that $ cart uses the Cart class. Use the-> symbol to indicate the method of the execution class.
The code is as follows:
Require ("cart. inc ");
$ Cart = new Cart;
$ Cart-> add_item ("10", 1 );
?>


Then, design a cart with a name. The carton is inherited from the cart, so the methods and attributes of the cart are also available, and the name method of the cart is added to the cart (maybe this attribute is more appropriate ).

In the following example, the sub-class Named_Cart uses extends to inherit its parent class Cart. Although the Named_Cart class does not have a method to add or reduce items, the parent class has everything due to its genetic characteristics.
The code is as follows:
// Program name: named_cart.inc
Require ("cart. inc ");
Class Named_Cart extends Cart {
Var $ owner;
Function set_owner ($ name ){
$ This-> owner = $ name;
}
}
?>


To use the cart type, see the following example. Of course, this is not a very good design. Every subclass always require its parent class, which will cause the server to bear on I/O. During the implementation, the entire series of classes can be located in the same program file, from the earliest class to the last child class, which is also convenient for future correction.
The code is as follows:
Require ("named_cart.inc ");
$ Ncart = new Named_Cart; // create a class variable
$ Ncart-> set_owner ("CyberRidder"); // Configure the name attribute of the class
Echo $ ncart-> owner; // display the name attributes of the class
$ Ncart-> add_item ("10", 1); // The method inherited from the parent class can also be used
?>


Therefore, PHP can become a CGI language with powerful class capabilities after using extends reserved words in PHP, coupled with good System Analysis and complete CRC cards (see object-oriented books.

Because PHP is a Script language, the source code of the program is visible. the black box of components in software engineering will not appear in the current PHP version. that is to say, the contents of all classes are not hidden. For software vendors, there is no way to protect the so-called software IC. for open groups, it is a good thing to have the source code. it is difficult to determine whether the software is right or not, however, PHP is still part of the Open Source Group, and the Zend engine may not be able to encapsulate classes in the future.

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.