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;
}
index.php
PHP Code:
<?
//auth the user
$id = sql_safe($_COOKIE[“id”]);
$pass = sql_safe($_COOKIE[“pass”]) www.phpsu.com
if(user_auth($id,$pass) == true)
{
//user is good
}
else
{
//user is bad
}
?> 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;
}
}
?> 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?
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教程
{
PHP Code:
public $id;
public $username;
public $password; www.phpsu.com
public $rank;
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;
}
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