您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

[转]类与PHP (II)

2025/4/6 2:44:34发布21次查看
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:
$varname=$value;
}
?>
so to change the value of particular variable of an instance we can:
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.
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:
set('bgcol','#aaaaaa');
$tbody->set('size',2);
?>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product