Jump to content
xisto Community
cityzen

An Easy Way To Use And Manage Your Php Classes On The Fly

Recommended Posts

The purpose of this short tutorial is to show how fast and easy can be to use classes within your PHP code, it may help you to manage and maintain easily your web applications.

 

What we’ll do here is to create a folder which will contain your php classes and a small script that will aloud your pages to load this classes on the fly avoiding repetitive declarations.

 

As example we’ll create two classes, one will manage the post form and variables, the other will display the html headers and footers.

 

Then we’ll simply call them trough a php page.

 

Test environement

To run this examples, it’s easier to do it in a WAMP (a full featured web laboratory installed in your computer). This topic assumes that you installed it as shown in this topic:

http://forums.xisto.com/topic/78567-optimize-your-virtual-hosts-separate-your-files-from-the-wamp/#entry517983

 

Otherwise, just make the appropriate changes in the paths used in the examples shown below.

 

Create the myClasses folder

This folder will contain all the classes you’ll use in you php code.

You can put the myClasses folder wherever you want. For security reasons you should put it outside of the folders publicly accessible, like the top level directory:

Test environement

Z:\home\yoursite\yourclasses\

Server

/home/yoursite/yourclasses/

 

Create the classloader.php file

This file will load automatically the classes stored in the ‘yourclasses’ file when needed.

Test environement

Z:\home\yoursite\classloader.php

Server

/home/yoursite/classloader.php

 

Code

<?phpfunction __autoload($class){require_once ( '/home/yoursite/yourclasses/'.$class.'.php' );}?>

Note: We put this code in a separate file to simplify improvements, like changes of method. You can also add here the session_start, or manage your page compression.

 

The class_test_post.php file

In this class you’ll have some functions who will read the infos posted and manage the post form.

 

Test environement

Z:\home\yoursite\yourclasses\class_test_post.php

Server

/home/yoursite/yourclasses/class_test_post.php

 

Code

<?php class class_test_post {var $yourname = "";var $yourmusic = "";function __construct(){if(isset($_POST['yourname']) || @$_POST['yourname']<>""){$this->yourname = $_POST['yourname'];$this->yourmusic = $_POST['yourmusic'];}}function display_info(){$string ="";if(isset($_POST['yourname']) || @$_POST['yourname']<>""){$string = "<b>Your name is </b>".$this->yourname." <br /><b>and you like </b>".$this->yourmusic."<hr/>";}else{$string = "Please fill in the form:<hr/>";}return $string;}function display_form(){return <<<DFR<form action="" method="post" >Your name <input name="yourname" value="$this->yourname" /><br/>Your music <input name="yourmusic" value="$this->yourmusic" /><br/><input type="submit" name="submit" value="Submit Form"</form>DFR;}}?>
Important: You noticed that the class name is the same as its name file without extention.

 

Create the class_test_html.php file

This class will simply display the header and footer of the html page. We’ll just pass the page title as variable.

Test environement

Z:\home\yoursite\yourclasses\class_test_html.php

Server

/home/yoursite/yourclasses/class_test_html.php

 

Code

<?php class class_test_html {static function display_header($pagetitle){return <<<DHD<html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>$pagetitle</title></head><body>DHD;}static function display_footer(){return <<<DFT</body></html>DFT;}}?>
Note: in case you need some google tips about the ‘<<<DHD’ ending with ‘DHD;’ this notation is called Heredoc.

 

Create the test_post.php file

This is the main page that will call and use the classes. Put it in a place accessible from your browser.

Test environement

Z:\home\yoursite\public_html\yoursite.com\test_post.php

Server

/home/yoursite/public_html/yoursite.com/test_post.php

 

Code

<?phpinclude("/home/yoursite/classloader.php");$post = new class_test_post();$str = class_test_html::display_header("The Title of Your Page");$str .= $post->display_info();$str .= $post->display_form();$str .= class_test_html::display_footer();echo $str;?>

Now, you can test it.

 

Some Explanations

The first row of this code includes the class loader file. This way you need only one row in each php page you’ll do, with whatever classes you’ll use. If you need to change the way you load classes, or add whatever code to every pages, like statistics, a session manager, or change the path to the class folder, you can do it at once in this file: the class loader.

 

The second row declares a class as an object, ‘$post’ will contain every function and variables enclosed in ‘class_test_post()’. You noticed that the classes have the same name as their file without extension, now you can imagine why...

From the third row until the sixth, we just store the contents returned by the classes before displaying it in the seventh row.

 

You noticed that we used two distinct ways to call the classes. Lets say a few words about this.

 

When we declared the $post as a new class_test_post, we did create an entire object that we can use with an arrow to call functions or set/return values.

Concerning the class_test_html, as we don’t really need any object, we can call the class functions directly just using the ‘::’ symbol. This is quite practical to store some home made functions, but don’t forget to declare this functions as ‘static’ in the class...

 

Ok, now you see how easy is to work with classes, I hope you liked it

Share this post


Link to post
Share on other sites

You are welcome Jhaslip,The classes are made to make development easier and more efficient, I guess that the most important is to feel confortable with whatever method you're using. When I try to recover some codes I did in a fast and dirty way it always becomes a nightmare... I hope that using classes will make me earn some time.If you are interested, I can developp more this subject while I learn more about it, let me know your feelings and thougts using classes :) Cheers

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.