another cool thing about classes is that PHP supports inheritence too, which means you can use existing classes to add to your own with the keyword
extends.
PHP Code:
<?
class person{
var $first_name;
var $last_name;
// constructor
function person($first_name,$last_name){
$this->first_name = $first_name;
$this->last_name = $last_name;
}
}
class user extends person{
var $username;
var $password;
// the person constructor does not execute unless you
// call it in this extended class. $this->person()
// we could call it in the user constructor, but i'm
// just creating its own.
function user($first_name,$last_name,$username,$password){
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->user_name = $username;
$this->password = $password;
}
}
// now when i create a new user, it has all the functions
// that the person class has along with new ones we create.
$myobject = new user("john","smith","jsmith","xx123xx");
echo "Welcome ". $myobject->first_name ." ". $myobject->last_name .";
?>
there is no example here that is too complicated to easily imagine doing it without classes, but it is a powerful tool when you need it.