选择显示字体大小

[转]类与php (i)


   classes and php

rod kreisler

the hardest concept i've tried to understand since beginning to use php was that of classes. i'd 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 couldn't 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! i'm 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. let's 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 couldn't all this be accomplished using regular variables and functions? yes it could, and if you only had one bike in your script it probably wouldn't 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 you'll be assured that a bicycle works the same way in each script!

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

i don't know about you, but when i'm 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 don't 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 &#36;text;
    var &#36;alink;
    var &#36;vlink;
    var &#36;link;
    var &#36;bgcol;
    var &#36;face;
    var &#36;size;
    var &#36;align;
    var &#36;valign;

}

?>

i&#39;m sure you&#39;re familiar with html so the variables should be self- explanatory. next i created a function for style called style:

<?php

class style {

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

    &#36;this->text=&#36;text;
    &#36;this->alink=&#36;alink;
    &#36;this->vlink=&#36;vlink;
    &#36;this->link=&#36;link;
    &#36;this->bgcol=&#36;bgcol;
    &#36;this->face=&#36;face;
    &#36;this->size=&#36;size;
    &#36;this->align=&#36;align;
    &#36;this->valign=&#36;valign;

}

}
?>

 


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons