|
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 Step 3
Okay, so we're done with setting variables. So what? We now have to actually put the class into use. The thing about classes that is great is that -- as I mentioned earlier -- there is a bunch of functions put together, and they use the same common variables. So let's create a new function that lets us do something with these variables -- display them.
<?php
The newly added function should be quite self-explanatory. It simply parses the HTML code with the variables contained inside the class. Step 4
What the class basically does is that it allows me to use the same "template" for every one of my files. Of course, this is not the only use that classes can do. They can do so many things that involve using similar types of variables. But now, here is an example of how I can use this class File #1: classes.php
<?php
File #2: index.php
<?php
I used $file->Display(); to call my function. This is the general syntax for calling anything within the class, whether it'd be a variable or a function. File #3: about.php
<?php
Completed
The example used in this tutorial, again, might be easily done with PHP includes. However, it was just a simple introduction to how classes work and how to use them. Thank you for reading. This tutorial is paginated, please navigate [ Steps 1-2 ] [ Steps 3-4 ] If you liked this tutorial, please help us expand by submitting links to your tutorial. This tutorial is an internal Tutorial Monkey tutorial. If you submit your tutorial, we will not claim any rights to it, and will simply link to it to help you gain more viewers. « Back |