Use of PHP classes Example code explanation_PHP tutorial

WBOY
Release: 2016-07-21 15:42:08
Original
1010 people have browsed it

PHP only has classes, methods, attributes, and single inheritance (extensions). For users who are not used to using C++, Java, Delphi and other object-oriented languages ​​to develop programs, you may wish to read a book about object-oriented concepts first. I believe it can bring a lot of gains.
The following example is a trolley class. As you can see, using class indicates 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 situation of a class, allowing the class itself to perform some steps according to the encapsulated method.

The $this class variable in the program is also a special variable in PHP, just like the $GLOBALS and $php_errormsg variables. The $this variable is only used in class categories to represent the class itself.

Copy code The code is as follows:

// Program name: cart.inc
class Cart {
var $items; // Cart class

// This method adds $num items to the cart (added to the $artnr variable)
function add_item ($artnr, $num ) {
$this->items[$artnr] += $num;
}

// This method reduces $num items from the cart (subtracted from the $artnr variable)
removefunction_item ($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num ;
return true;
} else {
return false;
}
}
}
?>


Yes Using a trolley can be done in a way similar to the example below. You can first save each class as an Include file, and then require or include it. When defining the variable $cart, use the reserved word new to indicate that $cart uses the Cart class. Use the -> symbol to indicate the execution of a class method.
Copy code The code is as follows:

require("cart.inc");
$cart = new Cart;
$cart->add_item("10", 1);
?>


Design a registered cart later. The registered trolley is inherited from the trolley, so the methods and attributes that the trolley has are also shared by the registered trolley, and the registered trolley has an additional name method (perhaps it is more appropriate to call it attributes) than the trolley.

As you can see from the following example, the subclass Named_Cart uses extends to inherit its parent class Cart. Although the Named_Cart class does not have methods to add items or reduce items, due to its genetic characteristics, it has everything the parent class has.
Copy code 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 named trolley class, please see the example below. Of course, this is not a very good design. Each subclass always requires its parent class, which will cause a burden on the server in terms of I/O. During implementation, the entire series of classes can be placed in the same program file, from the earliest ancestor class to the last descendant class, which also facilitates future revisions.
Copy code The code is as follows:

require("named_cart.inc");
$ncart = new Named_Cart; // Create a class variable
$ncart->set_owner ("CyberRidder"); // Configure the named attribute of the class
echo $ncart->owner; // Display the class's Named attribute
$ncart->add_item ("10", 1); // The method of inheriting from the parent class can also be used
?>


Therefore, after using extends reserved words in PHP, coupled with good system analysis and complete CRC card design (see object-oriented books for details), PHP can become a CGI language with powerful class capabilities. .

Because PHP is a script language (Script), the program source code is visible. The component black box in software engineering will not appear in the current PHP version. In other words, all classes are not hidden. its content. For the software industry, there is no way to protect the so-called software IC. From the perspective of the open community, having source code is a good thing. As for what is right and what is wrong, it is difficult to judge, but currently PHP is still a part of the Open Source community. Molecule, maybe in the future Zend engine can achieve the function of class encapsulation.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321065.htmlTechArticlePHP only has classes, methods, attributes, and single inheritance (extensions). For users who are not used to using C++, Java, Delphi and other object-oriented languages ​​to develop programs, you might as well...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template