Left Quote    Knowledge speaks, but wisdom listens.
- Jimi Hendrix    
Right Quote
[login] | [Register]

functions, classes, and objects -- oh my!

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


most people seem to think they first need to tell you why you should bother with all this before they tell you how to do it, but i'm not not going to do that. you certainly do not need to write and use classes in php, but if you are here then chances are you want to know some more about it. if you read this and decide that you have no reason to write your own classes, then you are probably right and i will not try to change your mind, but hopefully the reason will be something other than not knowing how!

in this tutorial i will guide you through the evolution of some php code as it passes from being a function through a static class and finally end up as an object. there is plenty more i could share with you on the theory behind objects, but all we are looking at here is giving you the understanding you need to be able to go out and write your own classes. if you decide you want to go into the theory, you can easily find some general oop books or web pages that will go into that.

a good place to start is by giving a shout out to classes and objects you may have already used, and by that i mean PEAR::DB. if you have not used PEAR::DB, it is a collection of classes that help make php code that accesses databases more portable across different database providers (such as mysql, oracle, or postgresql). the first thing you do with PEAR::DB is connect to a database:

some time after that you probably want to get some information out of the database:

both of those examples are using functions from PEAR's DB class. somewhere inside PEAR, both connect() and query() are defined as functions inside of a class named DB. this may be a little confusing now, but i haven't really explained anything to you yet. keep PEAR::DB in the back of your mind for now, and we will go thorugh a review of (or introduction to, depending where you're coming from!) functions.

functions are important to us here because they are commonly used within classes. i am assuming that you have at least some exposure to functions so this will just be quick. for this example, we will be looking at a few functions that will help us build an html form. i've actually used these functions on my website already, so you may find them useful. we will continue using this example throughout the tutorial, so if you hang in there you will also see a couple versions using classes (i also used or am using those versions on my site).

most forms have input tags in them, so we will start with a function that builds an input tag, which we will call forminput. The code would be as follows:

the actual code there is wrapped inside braces that follow the function keyword. this means that when php parses this code, it will remember it with the name of the function (in this case, forminput). the comment below the function shows how it would be used. inside php code, you would call the name of the function, and php understands that to mean what we have defined it to mean.

notice we are also passing three values ('fieldname', 'defaultvalue', and 25) through our function call. these values get assigned (in order) to the variables $name, $value, and $maxlength inside the function. if you look at the function code you can see that we are referencing these values.

you may have expected to see an echo or print command in the function so that it would actually print the code for an html input tag, but instead we see the return command. all functions may have the return command (though it is not always needed), and it is also legal to use it by itself. since we follow the return command with a value, this value will actually be sent back as the value of the function. so the code in the usage comment receives the value returned back from forminput and then prints it out. we could just as easily have made the function use an echo statement and then replaced the = when we call it with a space, but this way you get to see what return does.

there are of course more advanced things you can do with functions, many of which are useful when writing classes, but this should be adequate for understanding what we will be looking at. we're ready to take this example and turn it into a static class!

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


  Subject: "Good job" Date: Feb 03 2008 at 9:26 am    
classes,object. ..start to became more clearly to me. Thank you !
You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

1 Total Comments
4.6 Rating of 5 (5 Votes)


Tutorial Options

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



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

"" Copyright © 2002-2021; All rights lefted, all lefts righted.
Privacy Policy