Function 如何在类中对函数进行分组

Function 如何在类中对函数进行分组,function,class,Function,Class,我不熟悉PHP如何在一个类中对函数进行分组,并在其他页面中使用它来减少键入这是针对单个页面的,如果我使用多个页面进行分页怎么办 这是我的代码 <?php include ('conn.php'); class sel { function sell() { if(isset($_POST['submit'])) { $id=$_POST['id']; $username = mysql_real_escape_string($_POST['username']); $pass

我不熟悉PHP如何在一个类中对函数进行分组,并在其他页面中使用它来减少键入这是针对单个页面的,如果我使用多个页面进行分页怎么办 这是我的代码

<?php

include ('conn.php');
class sel
{
function sell()
{
if(isset($_POST['submit']))
{
$id=$_POST['id'];   
$username = mysql_real_escape_string($_POST['username']);   
$password = mysql_real_escape_string($_POST['password']);
$result = $myDBObject -> select('*', 'admin', 'username = "'.$username.'" and
  password ="'.$password.'"');
$x = $rdata['id'];
echo $x;
 $_SESSION['username'] =$id;
echo $_SESSION['username'];
if(! $rdata)
{
echo "Enter your username and password correctly";
}
else
{

echo '<script type="text/javascript">
     document.location="list.php";
     </script>';

}
}
}
}
$myDBObject = new sel();
$myDBObject->sell();
?>
//嗨

class ClassName {
    static function funcName1(){
        funcBody1;
    }

    static function funcName2(){
        funcBody2;
    }
}
//要使用函数,只需执行以下操作:

ClasseName::funcName1();
ClasseName::funcName2();

您可以使用下面提到的数据库类

class DBConnect
{
private $host = 'localhost';
private $user = 'user';
private $pass = 'pass';
private $db = 'mydb';

public function __construct(){
    $conn = mysql_connect($this -> host, $this -> user, $this -> pass);
    $sql = mysql_select_db($this -> db, $conn) or die(mysql_error());
}

// select query
private function select($select, $table, $where){
    $sql = "select ".$select." from ".$table." where ".$where;
    $result = mysql_query($sql);
    return $rdata = mysql_fetch_array($result);
}

// update query
private function update($update, $table, $where){
    // code here
}
}
需要时,创建类的对象并调用函数,如下所示

require 'dbConnect.php';    // assuming, you have included the database class in this file
$myDBObject = new DBConnect();
$username = mysql_real_escape_string($_POST['username']);   // mysql escaping
$password = mysql_real_escape_string($_POST['password']);   // mysql escaping
// select required data
$result = $myDBObject -> select('*', 'admin', 'username = "'.$username.'" and password = "'.$password.'"');
上述类中描述的函数用于演示。您可以在类中包含其他函数以及数据转义功能


仅供参考:
mysql
已弃用。改用
mysqli
PDO

您的努力是如此彻底,以至于您甚至没有定义一个函数!我也不明白这个问题到底在问什么。在一个完全不相关的注释中,您已经使用来自post的用户名和密码向自己发起了一些非常可怕的SQL注入攻击@乔治加沙古达什维利是的,我是新来的programming@MattFletcher我想在代码中使用函数和类,并对它们进行分组,这也是我的问题。我想你还不太明白类是用来做什么的——它们肯定不会在这种情况下重复使用代码,这是肯定的。也许现在最好不要担心OOP(先跑后走!),我仍然不知道你想把哪一点组合在一起。。。你在用什么?数据库连接代码或登录验证。。。或者别的什么?这有(破译代码!)拼写错误,还有
functbody1位很容易引起误解,因为它们没有注释。。。但是非代码位。。。是注释:P.也没有说代码应该包含在哪里,或者它将返回什么,或者OP将如何实际使用它。另外,它没有说明为什么会有人创建一个充满静态函数的类。只是不必要的额外步骤。我想我对你的回答责骂够了,对不起哈:)
class DBConnect
{
private $host = 'localhost';
private $user = 'user';
private $pass = 'pass';
private $db = 'mydb';

public function __construct(){
    $conn = mysql_connect($this -> host, $this -> user, $this -> pass);
    $sql = mysql_select_db($this -> db, $conn) or die(mysql_error());
}

// select query
private function select($select, $table, $where){
    $sql = "select ".$select." from ".$table." where ".$where;
    $result = mysql_query($sql);
    return $rdata = mysql_fetch_array($result);
}

// update query
private function update($update, $table, $where){
    // code here
}
}
require 'dbConnect.php';    // assuming, you have included the database class in this file
$myDBObject = new DBConnect();
$username = mysql_real_escape_string($_POST['username']);   // mysql escaping
$password = mysql_real_escape_string($_POST['password']);   // mysql escaping
// select required data
$result = $myDBObject -> select('*', 'admin', 'username = "'.$username.'" and password = "'.$password.'"');