PHP is a very powerful programming language which is favored more than many other languages when it comes to web functionality. In a personal viewpoint after learning functions and then on to classes you can start building much more advanced scripts and the only limit is your imagination.
First lets get an basic understanding on how functions and classes work together. A class would refer to a “vehicle” in whole, and each function would be each “component” to make that vehicle run. Classes can not work correctly without functions, however you can run functions without a parent Class. Since this is the case, lets first build a function to see how they work.
You can review more code snippits on PHP Code Examples
The only prerequisites needed to follow along with this tutorial would either be a server (online) or a offline “localhosted” service. If you would look for a easily deployable PHP install check out: http://www.apachefriends.org. Their packages support: Linux, Windows and OSX.
After you have a enviroment to work in lets start off by creating a file in your server’s live directory called: “helloworld.php”. Then proceed to write some code as follows:
1 2 3 4 5 6 7 8 9 10 11 | <?php /* Lets build our function below! This is a basic print function */ function helloWorld() { print 'Hello everyone! Check out my small function!'; } /* Now we will call our inline function and invoke the print! */ helloWorld(); ?> |
Granted that is very simple, but we need to start somewhere. Lets now more fancy and invoke some parameters to this function. You can either EDIT the previous file or create another, but lets modify some code to print some dynamic text.
1 2 3 4 5 6 7 8 9 10 11 | <?php /* Lets build our function below! This is a basic print function */ function helloWorld($name) { print 'Hello everyone! My name is ' . $name . '. Check out my small function!'; } /* Now we will call our inline function and invoke the print! */ helloWorld('Jonathan'); ?> |
That following script should print out what ever is inside the “function call” which in our case is “Jonathan”. This again was very basic but can easily have IF ELSE statements, or other variables passed into a function. There is something to remember about functions, is that any items within the function can not be accessed from outside. Any internal variables are only accessible from within. Once familiar with building functions you will notice how much easier they turn code into. Lets see a real world use of a more complex function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php /* connect to a database */ require("db.inc.php"); /* build a mysql_query function that can be used multiple times without duplicating queries. */ function query($query){ return mysql_query($query); } /* turn the query into a variable */ $results = query("SELECT * FROM users"); /* lets loop through the results and print a user name */ while($row = mysql_fetch_assoc($results)) { print $row['name']; } ?> |
Lets now learn up on “classes”! Again this could be considered the whole “automobile” or full unit with all its functions. One example where i use classes would be for “database” functions. Anything that interacts with the database will be within that class.
First we would need to figure out what our class will be doing, for my particular instance we will be continuing the database “theme” and create a “database” class. Lets create a new file called “database.class.php” and put the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php class MySQLDB { /* connect to a database */ require("db.inc.php"); /* build a mysql_query function that can be used multiple times without duplicating queries. */ function query($query){ return mysql_query($query); } }; /* Create database connection */ $database = new MySQLDB; ?> |
Notice in the script the function “query” is not being executed anywhere in that snippit, the reason behind that is normally you would be including your “classes” into your documents rather than inline coding. This object oriented coding allows for you to break your code into “building blocks” which are easier to maintain. In our example you would be keeping all your database functions in one spot.
Now, you can not just “call” the function by just typing: function query(); anymore. Now since its in a class you will now be using the line: $database->query(); which again helps keep all your code more organized.
Hopefully this has helped some, and feel free to ask questions or help in regards to my code. I tried to stay simple but this can lead to a very hard and complicating subject.
IF you would like to know more and dont mind reading documentation please read up on:
Functions: PHP – Functions
Classes & Objects: PHP – Classes & Objects




