+PC9kaXY+PGRpdiBjbGFzcz0ib21lZ2EgZ3JpZF83IiBpZD0iaGVhZGVyX3JpZ2h0Ij4KPHVsPgo8bGk+PGEgaHJlZj0iaHR0cHM6Ly92cHNkaW1lLmNvbS8iPlZQUyBXZWIgSG9zdGluZzwvYT48L2xpPgo8L3VsPg==

How To Use PHP Classes

PHP is an object oriented scripting language. What does that mean? It means that we have a great way to organise our code and a great way to re-use chunks of code.

The whole point of an object oriented language is to make everything better. The main problem is that object oriented PHP is a difficult concept to grasp if you have never used it before.

Why use object oriented PHP at all?

In times gone by before the days of object oriented PHP, everything would be put into functions. Each function would be a chunk of code that completes a task and you call it from elsewhere in your code, you could send it values, variables etc. Now functions are all very well, and are great for organising small projects but when you get into larger projects you will start running into problems.

Object oriented programming really shows its strengths when you start to build bigger, more complex projects. Mass organisation becomes easier and more robust. Teams of developers can operate independently with little fear of interfering with each others variable names. This kind of robustness simply isn’t possible using functions.

How to use object oriented PHP

Object oriented programming revolves around classes. Classes are basically the containers for your functions….Except to make things a bit more complicated the functions aren’t called functions in object orientated PHP, they are called methods.

Classes are created in much the¬†same way as functions are in non object oriented PHP except instead of the keyword ‘function’ we use ‘class’:

<?php

class first{

//do something

}

?>

You can then use your class within your code by creating a new instance of it:

<?php

$result = new first();

?>

Now obviously our class doesn’t do anything because we haven’t added anything to our class but we have created a basic class.

How to make the class do something

In order to make the class do something we need to add a few things to it. We need to add variables(called properties in OOP) that we can use and functions(called methods in OOP) that will do the work for us.

In Object oriented we usually have two standard functions at a minimum. These two properties are the setter and getter functions.

<?php
class first{
var $name;
function set_name($new_name) {
$this->name = $new_name;
}

function get_name() {
return $this->name;
}
}
?>

All this probably looks a bit confusing but its all really quite simple. It all looks confusing because of $this->…..$this is simply a built in variable that points to the current object. PHP knows exactly what to do when it comes across this.

Lets use our PHP class

Now we have a fully functioning class, one that actually does something. So how do we use it? We create an instance of the object in the servers memory:

<?php

$andy = new first();

$steve = new first();

?>

We now have two handles to our class. Both use the same class, just a different instance of it. Seeing this it slowly becomes clear how much simpler your code could become. You can use the same class as many times as you like and just set different handles to each instance and do different things to each instance. For example we can set a different name to each one:

<?php

$andy = new first();

$steve = new first();

$andy-> set_name("Andy Kay");

$steve->set_name("Steven Smith");

?>

Now this code doesn’t actually display anything on the screen but we have set two objects up and set the names of the two objects. It should be noted that $andy and $steve are completely separate objects, the only thing that relates them is that they use the same class but they are completely different objects in the eyes of PHP.

How to get at the results?

When accessing methods and properties of a class, you use the arrow  (->) operator. So to get at our names that we set earlier:

<?php

$andy = new first();

$steve = new first();

$andy-> set_name("Andy Kay");

$steve->set_name("Steven Smith");

echo $andy->get_name() . "<br />";

echo $andy->get_name();

?>

These are the basics but as you become more involved you will realise the power of OOP. If you want to get anywhere in PHP you need to be using an object oriented approach.

March 20th, 2011

Definitions

Dedicated Hosting

With dedicated hosting, your web site is hosted on a dedicated server.

Dedicated hosting is the most expensive option. This option is best suited for large web sites with high traffic, and web sites that use special software.

You should expect dedicated hosting to be very powerful and secure, with almost unlimited software solutions.

Free Hosting

…Some ISPs offer free web hosting.

Free web hosting is best suited for small sites with low traffic, like personal sites. It is not recommended for high traffic or for real business. Technical support is often limited, and technical options are few.

Very often you cannot use your own domain name at a free site. You have to use a name provided by your host like http://www.freesite.com/users/~yoursite.htm. This is hard to type, hard to remember, and not very professional…

Directory of best web hosting resources!

Apache Help MySQL Tutorials and help pages PHP Tutorials And Help