User-defined/custom class, is also necessary to learn php one of the conditions. The php class, and other object-oriented language comparison, was quite simple. php only class (class), method (method), property, single-level inheritance(extensions), and so on. Not accustomed to the use of c++, java, delphi, and other object-oriented language to develop procedures for the user, may wish to read about the concept of object-oriented books, I believe we can have a lot of harvest.
The following is an example of the type of cart class. Can see that the use of that word 'class' to mean that it is a class. In the class of function, such as 'add_item' said such an function. Ways to package the actual type of situation to deal with for such a good package, according to their own approach to the implementation of a number of steps.
The program $this variables and $globals and $php_errormsg two variables, in php it's a special variable. $this variable only use in the class, that indicate the class its own.
// 程序名: cart.inc
class cart {
var $items; // 手推车类
// 本方法加入 $num 件物品到手推车中 (加到 $artnr 变量)
function add_item ($artnr, $num) {
$this->items[$artnr] += $num;
}
// 本方法从手推车减少 $num 件物品 (从 $artnr 变量减掉)
function remove_item ($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
return true;
} else {
return false;
}
}
}
?>
To use the cart can be used under similar cases. Each of the class save as an include files, and then use 'include' or 'require' the file. When define $cart as variables, to use the 'new' reserved words,mean $cart use as cart class. Use -> symbols, to implementation the class functions.
require(”cart.inc”);
$cart = new cart;
$cart->add_item(”10″, 1);
?>
After that we re-design a 'name-tag cart'. 'Name-tag cart' inherit from 'cart', so 'name-tag cart' also have 'cart' properties and methods, but 'name-tag cart' can add more method or properties .
From the case, we can see that the use of named_cart subclass extends to the succession of his father-type cart. Although there is no increase or deduce in the class named_cart functions, but due to the genetic characteristics of the father of some types of things it has.
// 程序名: named_cart.inc
require(”cart.inc”);
class named_cart extends cart {
var $owner;
function set_owner ($name) {
$this->owner = $name;
}
}
?>
To the use of named-cart class, see the following example. Of course, this is not good design, each sub-class have been require its parent, can cause the server i / o above the burden. In practice, the whole family class will be in the same program files, the first from the father-class to children and grandchildren class, but also facilitate future amendments.
require(”named_cart.inc”);
$ncart = new named_cart; // 建立类变量
$ncart->set_owner (”cyberridder”); // 配置类的记名属性
echo $ncart->owner; // 显示类的记名属性
$ncart->add_item (”10″, 1); // 从父类遗传的方法也可使用
?>
As a result, in PHP reservation extends the use of the word, together with good analysis and complete crc card (see Object-oriented books) after the design, php can be turned into a powerful category cgi language ability.
PHP scripting language is due to the (script), therefore, source code can be seen in software engineering components in the black box and not in the current version of php arise, that is, in fact all of the categories do not hide from it. The software industry is concerned, there is no way to protect the so-called software ic, standing on an open group, but active code is a good thing, As for the charge, it would be difficult to determine, but php or open source group a Who, perhaps in the future zend engine can do the type of package will not necessarily function.
Translate By
CCH