PHP only has categories (class), methods (method), attributes, and single inheritance (extensions), etc. For users who are not used to using object-oriented languages such as C++, Java, and Delphi to develop programs, you might as well read the book about object-oriented concepts first, which I believe can bring a lot of benefits.
The following example is the trolley class. As you can see, use class to indicate that it is a class category. Functions in the category, such as add_item, represent a method of the class. Methods can encapsulate the actual processing situation of the class, allowing the class 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 two variables $GLOBALS and $php_errormsg. The $this variable is only used in the class category and represents the class itself.
<?php
// Program name:
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 trolley (decreased 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 save each class as an Include file first, and then require or include it. When defining the variable $cart, use the reserved word of new to indicate that $cart uses the Cart class. Use the -> symbol to represent the method of executing the class.
<?php
require("");
$cart = new Cart;
$cart->add_item("10", 1);
?>
Later, a registered hand trolley was designed. Registered trolleys were inherited from trolleys, so there are also registered trolleys, and registered trolleys have methods and attributes, while registered trolleys have added names to trolleys (perhaps the attributes of this name are more appropriate).
As you can see from the following example, the subclass Named_Cart uses extends to inherit its parent class Cart. Although there is no way to add items or reduce items in the Named_Cart class, due to its genetic characteristics, there are some things in the parent class.
<?php
// Program name: named_cart.inc
require("");
class Named_Cart extends Cart {
var $owner;
function set_owner ($name) {
$this->owner = $name;
}
}
?>
To use the registered trolley class, see the example below. Of course this is not a good design. Each subclass always requires its parent class, which will cause the server to burden on I/O. During implementation, the entire series of classes can be placed in the same program file, from the earliest class to the last class, which is also convenient for future correction.
<?php
require("named_cart.inc");
$ncart = new Named_Cart; // Create class variable
$ncart->set_owner ("CyberRidder"); //Configuration class's registered attribute
echo $ncart->owner; // Show the class's registered attributes
$ncart->add_item ("10", 1); // Methods that inherit from parent class can also be used
?>
Therefore, after using extends reserved words in PHP, coupled with good system analysis and complete CRC cards (see object-oriented books for details) design, PHP can become a CGI language with powerful class capabilities.
Since PHP is a scripting language (Script), the program source code is visible. The component black box in software engineering will not appear in the current PHP version, which means that all classes actually do not hide its content. For software operators, there is no way to protect the so-called software IC. As for open groups, having source code is a good thing. As for which one is right or wrong, it is difficult to determine. However, PHP is still a part of the Open Source group. Perhaps the Zend engine can achieve class encapsulation functions in the future.
The following example is the trolley class. As you can see, use class to indicate that it is a class category. Functions in the category, such as add_item, represent a method of the class. Methods can encapsulate the actual processing situation of the class, allowing the class 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 two variables $GLOBALS and $php_errormsg. The $this variable is only used in the class category and represents the class itself.
Copy the codeThe code is as follows:
<?php
// Program name:
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 trolley (decreased 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 save each class as an Include file first, and then require or include it. When defining the variable $cart, use the reserved word of new to indicate that $cart uses the Cart class. Use the -> symbol to represent the method of executing the class.
Copy the codeThe code is as follows:
<?php
require("");
$cart = new Cart;
$cart->add_item("10", 1);
?>
Later, a registered hand trolley was designed. Registered trolleys were inherited from trolleys, so there are also registered trolleys, and registered trolleys have methods and attributes, while registered trolleys have added names to trolleys (perhaps the attributes of this name are more appropriate).
As you can see from the following example, the subclass Named_Cart uses extends to inherit its parent class Cart. Although there is no way to add items or reduce items in the Named_Cart class, due to its genetic characteristics, there are some things in the parent class.
Copy the codeThe code is as follows:
<?php
// Program name: named_cart.inc
require("");
class Named_Cart extends Cart {
var $owner;
function set_owner ($name) {
$this->owner = $name;
}
}
?>
To use the registered trolley class, see the example below. Of course this is not a good design. Each subclass always requires its parent class, which will cause the server to burden on I/O. During implementation, the entire series of classes can be placed in the same program file, from the earliest class to the last class, which is also convenient for future correction.
Copy the codeThe code is as follows:
<?php
require("named_cart.inc");
$ncart = new Named_Cart; // Create class variable
$ncart->set_owner ("CyberRidder"); //Configuration class's registered attribute
echo $ncart->owner; // Show the class's registered attributes
$ncart->add_item ("10", 1); // Methods that inherit from parent class can also be used
?>
Therefore, after using extends reserved words in PHP, coupled with good system analysis and complete CRC cards (see object-oriented books for details) design, PHP can become a CGI language with powerful class capabilities.
Since PHP is a scripting language (Script), the program source code is visible. The component black box in software engineering will not appear in the current PHP version, which means that all classes actually do not hide its content. For software operators, there is no way to protect the so-called software IC. As for open groups, having source code is a good thing. As for which one is right or wrong, it is difficult to determine. However, PHP is still a part of the Open Source group. Perhaps the Zend engine can achieve class encapsulation functions in the future.