Search
Left Quote    I start where the last man left off.
- Thomas A. Edison    
Right Quote
 
[login] | [Register]
 

functions, classes, and objects -- oh my!

by: misterhaan
Page: 2 of 5
(View All)

classes

to put things simply, a static class is a collection of functions grouped together under a single class name. for this to make sense we should probably add another function. this function writes out an option tag for use inside a select tag, and selects it if $default is the same as $value:


now let's put both these into a static class named FORM. names for classes have the same restrictions as names for functions (which we haven't discussed here). we'll start just by defining the empty class:


looks rather a lot like a function definition doesn't it? the differences are that it says class instead of function, there are no parenthesis with variables in them, and we will actually be putting functions inside it rather than direct code. speaking of that, let's add our two functions:


now that we've put these functions inside the class, we need to call them differently. instead of just saying forminput, we need to say where it comes from and say FORM::forminput. all static calls to functions take this form: class name, two colons, then the function name. if you recall, DB::connect follows this form, and yes it is a static function call.

since we are using the name of the class to call these functions, there names are a little redundant (as we have to type FORM::form...), so let's drop 'form' and change the functions to input() and option(). If we had named our functions in this way before, that would have been a poor choice because 'input' or 'option' doesn't go quite far enough to tell you what it means. When we put it inside of a class though, it's identity depends on that class, so you no longer look at input() -- you look at FORM::input(), as input() by itself has no meaning. even while writing php code inside the FORM class, we still need to refer to any of its functions in this way -- if we wanted to call the input function from within the option function (which wouldn't actually make sense to do, but nevermind that), we would still have to say FORM::input so that php can find it.

static classes give us a way of grouping functions together. the function names only have meaning when referenced through the class name such as FORM::input(). this means that should you have a reason to, you can define an input() function that belongs to a different class or even one that does not belong to a class and still use it at the same time as FORM::input(). here is the code for our FORM class again, but with the function names changed:


the only changes we've made from our original functions is to put them inside a class and change their names--we now have FORM::input() instead of forminput() and FORM::option() instead of formoption(). but this isn't all that classes can do! we can also make classes that are not static but instead remember information between calls to functions from that class. in the next section we'll add to our FORM class and use this.



1  |  2  |  3  |  4  |  5  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

40,435 Views
1 Total Comments
4.6 Rating of 5 (5 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank