ll和 php,[转]类与PHP (II)-PHP教程,PHP应用

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:

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:

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).

lets create a style for table headers that has some slightly different attributes.

$theader= new style;

$theader->set(text,#0000ff);

$theader->set(bgcol,#000000);

?>

there, thats 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 ill make the text smaller:

$tbody=new style;

$tbody->set(bgcol,#aaaaaa);

$tbody->set(size,2);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值