免费邮箱 |加入收藏 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > PHP专区 > PHP教程

类与对象

时间:2011-05-16 16:00:42  来源:站内  作者:潘春会
, "PEACH");
echo
$eFruits->APPLE . ",";
echo
$eFruits->ORANGE . ",";
echo
$eFruits->PEACH . "n";

$eBeers = new DefinedEnum("GUINESS" => 25, "MIRROR_POND" => 49);
echo
$eBeers->GUINESS . ",";
echo
$eBeers->MIRROR_POND . "n";

$eFlags = new FlagsEnum("HAS_ADMIN", "HAS_SUPER", "HAS_POWER", "HAS_GUEST");
echo
$eFlags->HAS_ADMIN . ",";
echo
$eFlags->HAS_SUPER . ",";
echo
$eFlags->HAS_POWER . ",";
echo
$eFlags->HAS_GUEST . "n";
?>
Will output:
1, 2, 3
25, 49
1,2,4,8 (or 1, 10, 100, 1000 in binary)

Jeffrey (09-Oct-2008 02:51)

 

 

 

Why should anyone learn what classes and objects are? The short answer is to clarify and simplify code. Take this regular script:

<?php
$item_name
= 'Widget 22';
$item_price = 4.90;
$item_qty = 2;
$item_total = ($item_price * $item_qty);
echo
"You ordered $item_qty $item_name @ $$item_price for a total of: $$item_total.";
?>

You ordered 2 Widget 22 @ $4.9 for a total of: $9.8.

You can see clearly that you have to "define and set" the data, "perform a calculation", and explicitly "write" the results - for a total of 5 written statements. But the more you look at it, the more it needs fixin'. If you attempt to do that, your code can get really ugly, really fast - and remember, this is just a simple script! Here's the same program in OOP with all the fixin's:
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门