文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>[转]类与PHP(II)

[转]类与PHP(II)

时间:2007-02-17  来源:PHP爱好者

Classes and PHP
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
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:
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
<?php $Basic = new Style; ?>
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

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

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
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:
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
<?php
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
Function Set($varname,$value) {
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
$this->$varname=$value;
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
}
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
?>
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
So to change the value of particular variable of an instance we can:
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
<?php $Basic->Set('size','2'); ?>
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
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).
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
Let's create a Style for table headers that has some slightly different attributes.
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
<?php
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
$Theader= new Style;
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
$Theader->Set('text','#0000FF');
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
$Theader->Set('bgcol','#000000');
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
?>
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
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:
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
<?php
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
$Tbody=new Style;
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
$Tbody->Set('bgcol','#AAAAAA');
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
$Tbody->Set('size',2);
c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys

c hina it power . c omk5Rs8pnVTPX1tUZI5x3R409Ys
?>
php爱好者站 http://www.phpfans.net PHP|MySQL|javascript|ajax|html.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载