PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Intro To Object Oriented
Your Ad Here

Intro To Object Oriented


Intro To Object OrientedOne thing you dont see too much, is tutorials on object oriented php. Now some may be familiar with object from Java and C++, and then on the other hand, many of you may have no clue what object is. For this tutorial, you need to be pretty fluent in php. Object can be very complex and very confusing, so have a good understanding of php is a must.

In object, you create classes, and within those classes functions. For instance I have a db class, which contains functions to do all my db work; ie, select queries, inserts, updates, and deletes. The idea behind object is to create classes, that are totally independent, meaning you can use the same class in different files, or websites, with out chaning the class.

So what's so good about object? I'll use my image upload script as an example. I have an upload script, that uploads an image, checks to see if an image with that name is already in the directory, and replace an spaces with underscore. this script is right around 80 lines of code. I then add in a sniplet of code to resize the image, and that makes it around 90 lines of code. now say I have 2 sites I need to use this on, both with different places to store the images, and each with a different size to make the image. Usually you have to have the file twice, then edit each one to fit the needs of each site. With object you create the class, and function (which is right around the same amount of code) but then whenever I want to use it, i do this

$upload = $obj_images->upload("Image", $Img_Root);
$resize = $obj_images->resize($upload, $Img_Root, "200", "200");

I make the class one time, then can use it on any site, in any file, with any upload directory, and any size by doing 2 lines of code, and changing a few parameters when I call the object; making life much easier.

Now lets learn how to create classes.

This is just a personal pref, but I like to make different files for each class I make, you dont have to do this, but it makes it much easier when you start getting a lot of classes. any who, about the class

<?
class new_class {

//Functions here

}
?>

Pretty simple. You say class then the name of that class then { }. Inside { } is where you would place all the functions in that class. The name of the class must be unique, like variables, you can not have two named the same.

The class part is pretty easy. So lets move on to the functions. I'll first go over the syntax, and how to call a class and function before we actually create one; which we will do in this tutorial :).

<?
class new_class {

function new_function() {
//function here
}
}
?>

Functions may not be a new thing to you. Using a function in object, is the same as using a function in regular php (which is called linear). The first thing we do here, is create our class, then our function. The naming of functions is a little different than classes. Your can have 2 functions with the same name, as long as they are in different classes. The function should be given a different name than the class name, UNLESS, you want that function to run right when you instantiate (include, which we will go over soon) that function.

When you name a function the same as the class name, you create what is called a constructor. The constructor must be the the first function, and have the same name as the class. So, say you have a class with 4 functions. When you instantiate that class, none of the function run until you call them, but if you have a constructor, it will run right when you instantiate it. For example, I have a db_qur class, with a constructor. That constructor works to connect to the database, because if Im using a query, I need a db connection. So right when I instantiate the db_qur class, the constructor runs and connects to the database, saving me one step.

Usually you will a pass values (which are used like $ variables) to the function, which you place inside the (), these need to each have different names within a funcion, but you can use the same name in a different function. Well go over this more when we write a class.

You can also create a variable that can be passed to other functions in that class, with out having to pass it through the function. confusing? yeah I know, I'll try to make this a painless as possible.

When you do this, you create a variable $this->var = foo. Ill give you two examples of this to make it as easy as possible.

In my db class I have a function to run a mysql query. in this, i set the mysql_query to $this->res, like this:

<?
$this->res = mysql_query($qur);
?>

In the same class, I have another function to do a mysql_num_rows. The function looks like this:

<?
function numRows(){
return mysql_num_rows($this->res);
}
?>

What this does is sets $this->res = to the result of the mysql_query, then I pass that to the numRows function, to get the number of results. This makes it where you dont have to pass values when you call the numRows function, the values have already been passed.

Another good use for this is in my db constructor, which connects to the database. First I have a file where I set the values for the database connection:

<?
$GLOBALS['db_host'] = "host"
$GLOBALS['db_user'] = "user"
$GLOBALS['db_pass'] = "pass"
$GLOBALS['db'] = "db"
?>

Then I include the file that has my db class in it. The constructor of this class connects to the database. Instead of passing each of the above values to the function, I do this

<?
function db_qur() {
$this->db_host = $GLOBALS['db_host'];
$this->db_user = $GLOBALS['db_user'];
$this->db_pass = $GLOBALS['db_pass'];
$this->db = $GLOBALS['db'];

$GLOBALS['connection'] = mysql_connect($this->db_host, $this->db_user, $this->db_pass)
or die( "Unable to connect to SQL server");
mysql_select_db($this->db)
or die( "Unable to select database");
}
?>

This automatically gets the values I set above for the connection, then uses them to connect, without having to pass them to the function.

Now we need to instantiate the class. This is VERY important. Since your classes are going to be in different files then the one you instantiate them in, you need to include the file with the classes. YOU MUST include the class file, BEFORE you instantiate the class!! The code will not work if you instantiate them before you include your class file.

<?
$my_obj = new class_name();
?>

This too is very simple. all you do is set a variable (this variable will be used whenever you call a function) then set it = to new object_name(). So say we want to instantiate the class db_qur this is how it would be done.

<?
$obj_db = new db_qur();
?>

now to call the function

<?
$new_fun = $new_obj->funtion_name();
?>

This probably looks kind of odd if you've never seen it before. Its actually very simple though.

First we set a variable.

Then that = the variable you used to instantiate the class

then ->function_name();. Remember I said you can pass values to the function? between the () is again where you pass them, i'll show you how to do that shortly.

Before we create a class and function, I'll show you all the steps together, so you can get a visual example of everything together.

<?
//create the class
class my_first_class {

//create the function
function new_function{} {
}
}

//instantiate the class
$obj_first_class = new my_first_class();

//class the function
$func = $obj_first_class->new_function();

?>


Author's URL: Nathanael Mitchell
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "Intro To Object Oriented"

Only registered users can write comment

No comments yet...