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
PHP 类入门实例
用 户定义的类,也是学好 php 所必备的条件之一。而 php 的类,和其它的面向对象语言比较起来,还算蛮单纯的。php 只有类别 (class)、方法 (method)、属性、以及单一继承 (extensions) 等。对不习惯使用 c++、java、delphi 等面向对象语言来开发程序的用户,不妨先阅读一下有关面向对象概念的书,相信可以带来许多的收获。
下面的范例是手推车类。可以看到,使用 class 表示它是一个类类别。在类别中的 function,例如 add_item 则表示该类的一个方法。方法可以封装类的实际处理情形,让该类自己能依封装好的方法来执行一些步骤。
程序中的 $this 类变量也和 $globals 及 $php_errormsg 两个变量一样,在 php 中属于特殊的变量。$this 变量只用在类类别中,表示类的本身。
// 程序名: 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;
}
}
}
?>
要使用手推车可以用类似下例的方式。可以先将每个类存成 include 文件,再将它 require 或 include 进来。在定义变量 $cart 时,要使用 new 的保留字,表示 $cart 使用 cart 类。使用 -> 符号,表示执行类的方法。
require(”cart.inc”);
$cart = new cart;
$cart->add_item(”10″, 1);
?>
之后再设计有记名的手推车。记名手推车从手推车遗传下来,因此手推车拥有的方法及属性,记名手推车也有,而记名手推车比手推车增加了名字的方法 (或许该称属性较恰当)。
从下例中可以看到,子类 named_cart 使用 extends 来继承其父类 cart。虽然 named_cart 类中没有增加物品及减少物品的方法,不过由于遗传的特性,父类有的东西它都有。
// 程序名: named_cart.inc
require(”cart.inc”);
class named_cart extends cart {
var $owner;
function set_owner ($name) {
$this->owner = $name;
}
}
?>
要使用记名手推车类,请看下面的范例。当然这不算太好的设计,每个子类都一直 require 它的父类,会造成服务器在 i/o 上面的负担。在实作时,可以将整个系列的类在同一个程序文件中,从最早的袓先类到最后的子孙类,也方便日后修正。
require(”named_cart.inc”);
$ncart = new named_cart; // 建立类变量
$ncart->set_owner (”cyberridder”); // 配置类的记名属性
echo $ncart->owner; // 显示类的记名属性
$ncart->add_item (”10″, 1); // 从父类遗传的方法也可使用
?>
因此,在 php 中使用了 extends 保留字,加上良好的系统分析与完整的 crc 卡片 (详见面向对象相关书籍) 设计之后,php 可变成拥有强大类能力的 cgi 语言。
php 由于是脚本语言 (script),因此程序源代码可见,在软件工程中的元件黑箱并不会在目前的 php 版本中出现,也就是说,全部的类其实没有隐藏起它的内容。对于软件业者而言,没有办法保护所谓的软件 ic,站在开放团体而言,反而有源代码是件好事,至于孰是孰非,就很难判定了,不过目前 php 还是 open source 团体的一份子,或许日后 zend 引擎可以做到类封装的功能也不一定。
No comments:
Post a Comment