[转]类与PHP (I)
程序员文章站
2022-05-15 17:19:08
...
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:
class Style {
var $text;
var $alink;
var $vlink;
var $link;
var $bgcol;
var $face;
var $size;
var $align;
var $valign;
}
?>
I'm sure you're familiar with HTML so the variables should be self- explanatory. Next I created a function for Style called Style:
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;
}
}
?>
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:
class Style {
var $text;
var $alink;
var $vlink;
var $link;
var $bgcol;
var $face;
var $size;
var $align;
var $valign;
}
?>
I'm sure you're familiar with HTML so the variables should be self- explanatory. Next I created a function for Style called Style:
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;
}
}
?>