欢迎光临
我们一直在努力

[转]类与PHP (I)-PHP教程,PHP应用

建站超值云服务器,限时71元/月

classes and php

rod kreisler

the hardest concept ive tried to understand since beginning to use php was that of classes. id never used a database engine but learning to use mysql, at least for the more basic functions, was a breeze. having never used oop before, classes were novel as well, but understanding the theory and why it was useful escaped me. i knew they must be powerful as "everything" is programmed using oop, but for the life of me, although i thought i understood the mechanics, i couldnt see the usefulness. then, just a few days ago, while trying to figure out how to do something with regular functions it hit me just how doing it with objects would make my job much simpler! im going to try to explain about them in plain english and hopefully help others like myself.

classes are nothing more than a collection of variables and functions acting on those variables. they provide a means of thinking about things in real world terms. in other words they describe an object. an object, or instance, of a class is an actual "living, breathing" structure of that class. lets say we want to describe a bicycle. a proper class of a bicycle might have the variables $pedals, $chain, $front wheel, $rear wheel, $brakes, and $handle_bars. functions of the bicycle would include stop(), accelerate(), coast(), turnleft() and turnright(). you can think of your script as the entity operating that bike. the function accelerate() could be passed an argument such as $braking_force and use that information along with the defined instance variables (probably $brakes and $wheels) and output some result back to your script.

interesting, but couldnt all this be accomplished using regular variables and functions? yes it could, and if you only had one bike in your script it probably wouldnt make much sense to define a class just for it. but what if you needed several bicycles? it could become quite complex keeping track of all those variables and making sure you pass the correct variables to the different functions, and using objects cuts down on the number of variables you need to pass because the function automatically has available to it all the variables describing the object the function is acting upon. also, class definitions are easily included in different scripts and youll be assured that a bicycle works the same way in each script!

lets create a class that i actually use on almost every page on my site and you might find useful, too.

i dont know about you, but when im writing a dynamic web page i hate to have to stop thinking about the logical flow to worry about properly formatting my html. as a consequence of this, i often end up with not so attractive pages because i dont want to worry about font faces and sizes, or background and text colors. the solution: using a php class to set html output attributes with functions to format the text!

i call the class "style". it contains the following variables that set important html attributes for formatting the output:

<?php

class style {

    var $text;
    var $alink;
    var $vlink;
    var $link;
    var $bgcol;
    var $face;
    var $size;
    var $align;
    var $valign;

}

?>

im sure youre familiar with html so the variables should be self- explanatory. next i created a function for style called style:

<?php

class style {

function style ($text="#000000",$alink="#aa00aa",
$vlink="#aa00aa",$link="#3333ff",
$bgcol="#999999",$face="book antiqua",$size=3,$align="center",$valign="top") {

    $this->text=$text;
    $this->alink=$alink;
    $this->vlink=$vlink;
    $this->link=$link;
    $this->bgcol=$bgcol;
    $this->face=$face;
    $this->size=$size;
    $this->align=$align;
    $this->valign=$valign;

}

}
?>

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » [转]类与PHP (I)-PHP教程,PHP应用
分享到: 更多 (0)