Introduction to PHP Classes (OOP)

February 28, 2008

When you start working with PHP, you’ll find that there’s many common tasks that you constantly have to type the same code for. Some of you may have figured out by now that making those tasks into a function will save you a little bit of time when you want to reuse that code. You simply declare a function and tell that function what information you want it to use by a method similar to this:

function MyFunction($variable){// Manipulate $variable here:echo $variable;}

And then you’d call the function when you needed it:

MyFunction("Hi, my name is phpfreak");

Using PHP’s Classes, you can start to put those functions into “containers” where they are stored and utilized. PHP’s Classes have been proven to be valuable, if you plan and code them properly.

The Class Structure

When you think of classes, I would almost encourage you to think of “back-end” Your code will be stored in the back-end of your website and the front-end will contain a very clean set of code that is easy to understand. If you put all of the complicated routine tasks into your class, then your script that is utilizing that class will be easier to understand because it will have a cleaner look. That’s not the only reason you would use a class, but that’s one reason I use them.

Alright, let’s take a look at a class.

<?class MyClass{var $email;// use a function without variablesfunction check_email(){
if(ereg("^.+@.+..+$", $this->email))return (true);elsereturn (false);}
// Use a function with variablesfunction image_strip($somehtml){

$somehtml = preg_replace("/(<img)(.*?)(>)/si", "", $somehtml);

return $somehtml;}}?>

The above class was derived from functions that I have found in our QuickCode Library. It’s a very simple class to use and we’ll break it down into pieces so that you can understand what’s going on here.

First, you need to declare a class:
class MyClass{

We are going to create some vars that may be used in any of the functions inside this class without requiring you to specify them for each function. We do this by “var $varname” see below for our $email example we’ll use.

// use a function without variablesfunction check_email(){
if(ereg("^.+@.+..+$", $this->email))
return (true);elsereturn (false);}

Looking at the function above, notice that I used “$this->email” $this-> is a special constructor that points internally into the class to a variable or another function. When we defined var $email above, we now have $this->email. You’ll see how to assign a value to $this->email when we actually use the script that calls the class.
Note: you can also use other functions inside the same class within other functions. Just use $this->function_name($vars); and other functions within the same class can utilize each other.
To give you an example that you can put just about anything you want in a class, we’ve made this a “general purpose” class with multiple different uses. Below, you’ll see a function that I have included in this class that strips an image tags out of a string. I also wanted to show you this class because it’s a different method of calling the function, this function defines that it will accept one variable and must have that variable defined when you call it. Again, I’ll explain that later. See the function below:

// Use a function with variablesfunction image_strip($somehtml){
$somehtml = preg_replace("/(<img)(.*?)(>)/si", "", $somehtml);

return $somehtml;}}

Now, we just close out our class and we’re ready to put it to use!

Let’s name this file as “clsMyClass.php”.
Using the Class in Your PHP Scripts

The first thing we have to do is include the class. There’s not a whole lot to it, just include it by using

<? include"clsMyClass.php";

We have our class included and we have to initalize the object by giving it a named variable. See how below:
$myclass=&new MyClass;

We have made an object called $myclass and now we can access the items inside the MyClass class that we previously made. So, first thing is first. We’ve got that “var $email” hanging out in MyClass and we’re going to assign a value to it. Let’s do this:

$myclass->email = "you@somewhere.com";

Keep in mind, you can do $myclass->email = $_POST[’email’]; or whatever you want to do with it.
Let’s run the email_check function inside the MyClass class.

$check_email = $myclass->check_email();if(!$check_email){
echo "The email address is not valid!";}

else {echo "The email address is valid!";}

We’ve simply assigned a variable to the check_email() function and then did some error checking based on that variable. Remember what I said before that if the check_email returns TRUE the $email we told it to check passed the test, and if it failed, it would return FALSE.

Looking at the if statement above, you’ll see that if an “!” or “Error” on the $check_email string is detected, we’ll echo “The email address is not valid!”, otherwise we’ll echo “The email address is valid!”.

Let’s take a look at how to use the image_strip function. First, we’ll create a string with some HTML inside of it.

$oldhtml = '<strong>I have an image here: <img src="/myimage.jpg" height="100" width=" 200" ALT="My Image">';

Now that we have $oldhtml and we know it has an image inside the HTML, let’s strip that image out and return the results:

$newhtml = $myclass->image_strip($oldhtml);

echo "Here is my old html $oldhtml <br /><br />";

echo "Here is my new html $newhtml <br /><br />";

When you take a look at $oldhtml and $newhtml, you should see that the image was stripped out succesfully.

See the source.

 

Post a comment

Name (required)

Mail (will not be published) (required)

Website

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word