PHP class Use instance code to explain _php tutorial

Source: Internet
Author: User
PHP has only categories (class), methods, attributes, and single inheritance (extensions). For users who are not accustomed to using C + +, Java, Delphi and other object-oriented languages to develop programs, read about the object-oriented concept of the book, I believe can bring a lot of gains.
The following example is a trolley class. As you can see, using class means that it is a class category. A function in a category, such as Add_item, represents a method of the class. Methods can encapsulate the actual processing of a class, allowing the class to perform some steps in a packaged way.

The $this class variables in the program are the same as the $GLOBALS and the $php _errormsg two variables, which are special variables in PHP. $this variables are used only in class categories to represent the class itself.
Copy CodeThe code is as follows:
Program Name: Cart.inc
Class Cart {
var $items; Trolley class

This method adds $num items to the trolley (add to $ARTNR variable)
function Add_item ($ARTNR, $num) {
$this->items[$artnr] + = $num;
}

This method reduces $num items (from the $ARTNR variable) from the trolley
function Remove_item ($ARTNR, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr]-= $num;
return true;
} else {
return false;
}
}
}
?>


A trolley can be used in a way similar to the following example. You can save each class as an include file before you require it or include it in. When you define a variable $cart, you use a reserved word for new, which means that the $cart use the cart class. The notation, which represents the method that executes the class, is used.
Copy CodeThe code is as follows:
Require ("Cart.inc");
$cart = new Cart;
$cart->add_item ("10", 1);
?>


Then a registered trolley is designed. A named trolley is inherited from a trolley, so the method and properties of the trolley are also available, and the name Cart adds the method (perhaps the property is more appropriate) than the cart.

As you can see from the following example, the subclass Named_cart uses extends to inherit its parent Cart. Although there is no way to add items and reduce items in the Named_cart class, there is something in the parent class because of its genetic properties.
Copy CodeThe 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 named Trolley class, see the example below. Of course, this is not very good design, each subclass has been require its parent class, will cause the server on the I/O burden. In practice, the entire series of classes can be in the same program file, from the earliest beloved Motherland class to the last descendant class, but also easy to fix later.
Copy CodeThe code is as follows:
Require ("Named_cart.inc");
$ncart = new Named_cart; Creating class variables
$ncart->set_owner ("Cyberridder"); To configure the named property of a class
Echo $ncart->owner; Display the named property of a class
$ncart->add_item ("10", 1); Inherited from the parent class can also be used
?>


So, with the use of extends reserved words in PHP, coupled with good system analysis and complete CRC cards (see object-oriented books), PHP became a CGI language with powerful class capabilities.

PHP is a scripting language (script), so the program source code is visible, in software engineering, the component black box does not appear in the current version of PHP, that is, all the classes do not actually hide its contents. For the software industry, there is no way to protect the so-called software IC, standing in the open community, but the source code is a good thing, as to who is not, it is difficult to determine, but at present, PHP is a part of the open Source community, perhaps in the future Zend engine can do class encapsulation function is not necessarily.

http://www.bkjia.com/PHPjc/321065.html www.bkjia.com true http://www.bkjia.com/PHPjc/321065.html techarticle PHP has only categories (class), methods, attributes, and single inheritance (extensions). For users who are not accustomed to the use of C + +, Java, Delphi and other object-oriented languages to develop programs, may ...

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