Class and PHP

Source: Internet
Author: User
Classesandphprodkreislerthehardestconceptivetriedtounderstandsincebeginningtouphpwasthatofclasses. IdneverusedadatabaseenginebutlearningtouseMySQL, atleastforth 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, For the life of me, although I thought I understood the mechanic, 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 wocould 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 wocould include Stop (), Accelerate (), Coast (), TurnLeft () and TurnRight (). you can think of your script as the entity operating that bike. the function Accelerate () cocould 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 cocould, 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 cocould 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. also, class definitions are easily encoded in different scrip Ts 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 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 shocould 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;



}



}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.