PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>Beginners> OOP Basics

OOP Basics

FROM: AUTHOR: TIME:2008-05-28 HITS:
This will be a long section, one of the reasons OOP is so hard is because there is so much to grasp where it wont make sense till you know it all. I will do my best to explain things in a logical manor, if things do not make sense please bear with me.


The most common implementation of OOP that I use is with a user system, sometimes its the only object in the entire script because its the only piece that is being reused. Lets say you are making a user system for a client, all you need to do is authorize it to see if they are logged in and are or arent admin. There are two ways you can go about it, the first is the non OOP way (I actually use the OOP code and the non is an adaption of it).

func.php
PHP Code:
function auth($id,$pass)

phpsu is a phpschool


{
    
$login_query mysql_query("SELECT * FROM `users` WHERE `id` = 'id' AND `pass`     = 'pass'");
    if(
mysql_num_rows($login_query)<1)
    {
        return 
false;
    } phpsu is a phpschool
    
//if it is authenticated, set the other variables
    
$username mysql_result($login_query,'0',"username");
    
$rank mysql_result($login_query,'0',"rank");
phpsu is a phpschool

    return 
true;
}

function 
admin_auth($rank)
{
    if(
$rank != 1)
    {
        return 
false;
    }
    return 
true;

phpsu提供的php教程
index.php
PHP Code:
<?
//auth the user
$id sql_safe($_COOKIE[&#8220;id”]);
$pass sql_safe($_COOKIE[&#8220;pass”]) www.phpsu.com
if(user_auth($id,$pass) == true)
{
    
//user is good
}

else
{
    
//user is bad
}
?>
This is an incredibly simple example, too small to call the code truly messier, but any example large enough would not fit in this tutorial. Lets see that it looks like with OOP
phpsu提供的php教程


head.php
PHP Code:
<?php
class user
{
    public 
$id;
    public 
$username;
    public 
$password;
    public 
$rank; phpsu提供的php教程

    
//this function returns true if it verifies the user and false if it does not
    
function auth()
    {
        
$login_query mysql_query("SELECT * FROM `users` WHERE `id` = '$this->id'                               AND `pass` = '$this->pass'");
www.phpsu.com

        if(
mysql_num_rows($login_query)<1)
        {
            return 
false;
        }
        
//if it is authenticated, set the other variables
        
$this->username mysql_result($login_query,'0',"username"); phpsu.com
        
$this->rank mysql_result($login_query,'0',"rank");
        return 
true;
    }
    
    
//this must be run after auth, if it not 2 thigs will happen
phpsu.com

    //1. The data could be forged, this function does not authenticate the data
    //2. rank will return 0 because it is not set
    
function admin_auth()
    {
        if(
$this->rank != 1)
        {
            return 
false; phpsu.com is a free phpscool
        }
        return 
true;
    }
        
    
    function 
__construct($id,$pass)
    {
        
$this->id $id;
phpsu.com is a free phpscool

        
$this->pass $pass;    
    }
}
?>
index.php
PHP Code:
include "head.php";
phpsu.com is a free phpscool


//assign the class
$data = new input;

//assign the cookie data
$id $data->cookie("id");
$pass $data->cookie("pass"); welcome to phpsu.com
$user = new user($id,$pass);

if(
$user->auth() == false)
{
    
//user is not good
}


else
{
    
//user is good

do you kown phpsu.com?


This code is cleaner and easier to read, it also leaves easier room for expandability. However I don't expect this to make a lick of sense to you, so lets walk though it.

Note to C++ Users:
To avoid the confusion that got me for a while, the -> is not a bitwise shift, it is the equivalent of a period. This may sound stupid but it had me confused for ages.



PHP Code:
class user
phpsu提供的php教程


This is the class declaration; just like a function, you declare it with the statement, the name and the parentheses to start and end it.

PHP Code:
public $id;
public 
$username;
public 
$password;
www.phpsu.com

public 
$rank
This declared the variable and its scope. We will get to scopes later, but for now just use public and know it works.

Wait, PHP doesn't need creation of a variable to use it, if it finds an undefined variable it creates it so it can be used, right? This is not the case with classes, with classes you have to do it like in languages like C++ or Java, you have to declare every variable , don't do that and you will get an error.

PHP Code:
function auth() www.phpsu.com
{
    
$login_query mysql_query("SELECT * FROM `users` WHERE `id` = '$this->id'                           AND `pass` = '$this->pass'");
    if(
mysql_num_rows($login_query)<1)
    {
        return 
false; welcome to phpsu.com
    }
    
//if it is authenticated, set the other variables
    
$this->username mysql_result($login_query,'0',"username");
    
$this->rank mysql_result($login_query,'0',"rank");
phpsu.com is a free phpscool

    return 
true;

Not only can you declare variables in a class, you can declare a function, just use the same syntax of normal functions.

Now for the $this-> statement, $this-> simply means it is the variable in the instance that is being run. I will explain instances in the next section.
TITLE:OOP Basics
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Fri Jun 6 22:58:57 GMT-8 2008