我是OOP的新手,我正在将我所有的网站代码都切换到OOP!我目前正在编写一个类,它可以获取用户的信息,并最终更新它
我使用的代码如下:
<?php
require("C:'wamp'www'postin''db_connection.php");
session_start();
class user {
public function __construct($userid, $connection, $information) {
$this->userid = $userid;
$this->connection = $connection;
$this->information = $information;
}
public function user_information($userid, $connection, $information) {
$query = "SELECT * FROM users WHERE id = :id";
$params = array(':id' => $userid);
try{
$stmt = $connection->prepare($query);
$result = $stmt->execute($params);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$columns = $stmt->fetch();
return $columns["$information"];
}
}
$username = new user($_SESSION["logged_in"], $connection, "username");
echo $username->user_information($_SESSION["logged_in"], $connection, "username");
?>
现在,正如您在最后两行代码中看到的那样(最后一行),我必须使用两次参数。基本上,第一个参数表示ID是什么,第二个参数表示$connection
是什么,而第三个参数是我想从数据库中获取的内容。那么我做错了什么?我是否定义了一些我不需要定义的东西?
编辑
以下内容也有效吗?
<?php
require("C:'wamp'www'postin''db_connection.php");
session_start();
class user {
public function user_information($userid, $connection, $information) {
$query = "SELECT * FROM users WHERE id = :id";
$params = array(':id' => $userid);
try{
$stmt = $connection->prepare($query);
$result = $stmt->execute($params);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$columns = $stmt->fetch();
return $columns["$information"];
}
}
$username = new user();
echo $username->user_information($_SESSION["logged_in"], $connection, "username");
?>
就像这是在properer,还是错了。。。?
如果user
类具有作为数据成员所需的所有信息,则user_information
不需要接受任何参数:
public function user_information() {
$query = "SELECT * FROM users WHERE id = :id";
$params = array(':id' => $this->userid);
try{
$stmt = $this->connection->prepare($query);
$result = $stmt->execute($params);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$columns = $stmt->fetch();
return $columns[$this->information];
}
由于您对类的工作方式和OOP有很多问题,我将尝试为您提供一些指导。建立你的班级没有标准的方法。根据什么属于类以及需要注入什么,你决定了什么去哪里。这只是告诉你,你不能把自己束缚住。你需要对它有一种感觉,并建立一个逻辑。我把你的课改了一遍,并添加了一些评论。希望这对你有所帮助。祝你好运
<?php
require ("C:'wamp'www'postin''db_connection.php");
session_start();
class user {
public $dbconnection;
public function __construct($connection) {
/**
* Your user class interacts with the database.
* Inject the connection here and set your
* global class variable.
*/
$this -> dbconnection = $connection;
}
public function user_information($userid, $column) {
/**
* The userid and column are specific for this
* method action. No need to set these variables
* in the global scope of the class.
*/
$query = "SELECT" . $column . " FROM users WHERE id = :id";
$params = array(':id' => $userid);
try {
$stmt = $this -> dbconnection -> prepare($query);
$stmt -> execute($params);
} catch(PDOException $ex) {
echo("Failed to run query: " . $ex -> getMessage());
}
$result = $stmt -> fetch();
return $result;
}
}
$username = new user($connection);
echo $username -> user_information($_SESSION["logged_in"], $information);
?>