|
PHP -> Object Oriented Programming -> Introduction to using classes
Introduction to using classes
- Apr 06 2007
This tutorial will teach you what classes are and how to use them effectively in your web application.
Tutorial Monkey is a database of technology-related tutorials. We write our own tutorials as well as linking to other ones in the web. If you would like to submit a link to your tutorial, feel free to do so. Just click on "submit" at the top of the page. Note: Images may result in loss of quality due to the fact that we must resize them to fit the design. You can click resized images to see its original image in a new window. Sponsors Important It is worth noting that this tutorial is paginated, and that the entire tutorial contains 2 pages. Just remember that the tutorial is not finished just after the first page! Before reading this tutorial, we recommend that you already know how functions and variables work. Classes are in general just a bunch of functions put together. The basics are not as hard to grasp as many people might say it is. Classes organize your code so that you can have the same interface to output but many different variables that are inputted. That explanation may not be very clear, but after reading through this tutorial, you will get it. In this tutorial, we will make a custom HTML "template" that we will generate by using a class. Although what this example will do can easily be done in PHP includes, I am using this as an example of how classes operate. Step 1
Declaring a class is just as simple as declaring a function:
<?php
the only difference is that there are no brackets to set any arguments. The "arguments" in a class is set within the class, before anything else is declared. For this example we will use a couple of arguments:
<?php
In a class, the arguments/variables must be set this way.. with a "var" prior to the variable. Step 2
Comparing it to a function, what we have so far is simply something similar to
<?php function generateHTML($title, $bgcolor, $heading, $content, $footer); ?>
What we will do next is create what is called a "constructor," which sets our default variables. Analyze the following code yourself before reading the explanations that will follow.
<?php
What basically happened was that in our function we set our default arguments, and then inside the function we declared that whatever arguments were set via the function would be used as the same value inside the entire class. $this is an object that allows you to point to a variable that was set in the beginning of the class. These variables can be set / used throughout the class. The constructor that we have just made basically sets the variables inside the class to either 1) the default, or 2) whatever it is when it is called (just like a function). For example, if I called the class now like this:
<?php
the above would set all the variables in the class to the default, because I did not specify any arguments. It is also worth noting that when there is a function that is the same name as the class, it is considered the constructor and when calling the class, you are also calling the constructor at the same time. If I wanted to set say-- the title -- to be different from the default, I would do something like:
<?php
instead of leaving out everything. It is exactly the same as calling a function. This tutorial is paginated, please navigate [ Steps 1-2 ] [ Steps 3-4 ] « Back |