快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

[转]类与PHP (II)

Classes and PHP

When you create a function within a class with the same name as the class that function will execute whenever you create an object of that class. This is called a \\'constructor.\\' It allows me to have default values for each attribute automatically whenever I create an object:

<?php $Basic = new Style; ?>

You define an instance of a class by giving it a name ($Basic) and assigning it "=new ClassName;".

You could also send different values for the variables as arguments when declaring the new Style, but if you declare a value you have to declare all of them that occur to the right of the one you declare (class functions work just like regular functions in this respect). Meaning if you set \\'text\\' to something different than the default given in Style, then you have to declare all the variables. There is an easier way. We can create a function that changes a single variable within the class:

<?php

Function Set($varname,$value) {

        $this->$varname=$value;

}

?>

So to change the value of particular variable of an instance we can:

<?php $Basic->Set(\\'size\\',\\'2\\'); ?>

You use the "->" operator to refer to a variable or a function of an instance. So the above tells the interpreter "Run function \\'Set()\\' of instance \\'$Basic\\'." It knows that "$Basic" is an instance of class "Styles" because we declared it so. Similarly we can refer to variables of an instance in the same manner (e.g. $Basic->text).

Let\\'s create a Style for table headers that has some slightly different attributes.

<?php

$Theader= new Style;
$Theader->Set(\\'text\\',\\'#0000FF\\');
$Theader->Set(\\'bgcol\\',\\'#000000\\');

?>

There, that\\'s good enough. Now my table header has blue text on a black background. I want my table body to be a slightly lighter gray than my main page, black is good for text, but maybe I\\'ll make the text smaller:

<?php

$Tbody=new Style;
$Tbody->Set(\\'bgcol\\',\\'#AAAAAA\\');
$Tbody->Set(\\'size\\',2);

?>





返回类别: 教程
上一教程: PHP中for循环语句的几种“变态”用法
下一教程: 第六节 访问属性和方式 [6]

您可以阅读与"[转]类与PHP (II)"相关的教程:
· [转]类与PHP (III)
· Win2000下IIS中配置PHP详细方式
· 在windows iis5下安装php4.0+mysql之我见
· 第十四节--命名空间 -- PHP5的类与对象 [14]
· 第十节--抽象方式和抽象类 -- PHP5的类与对象 [10]
    微笑服务 优质保证 索取样品