Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP类参数使用了两次_Php_Oop - Fatal编程技术网

PHP类参数使用了两次

PHP类参数使用了两次,php,oop,Php,Oop,我是OOP新手,我正在将我所有的网站代码切换到OOP!我目前正在编写一个类来获取用户的信息,并最终对其进行更新 我使用的代码如下: <?php require("C:\wamp\www\postin'\db_connection.php"); session_start(); class user { public function __construct($userid, $connection, $information) {

我是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");
?>

类似于properer中的这一点,还是错误的…?

如果
用户
类拥有作为数据成员所需的所有信息,那么
用户信息
不需要接受任何参数:

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);
?>


我还需要这样做吗:$username=新用户($_SESSION[“logged_in”],$connection,“username”);?那么_构造函数的意义是什么呢?构造函数是用来创建基本对象的。基本上你决定你的基本对象是什么样子。想象一下你有一门狗课。基本特征在构造器中,如四爪、尾巴、吠叫和毛皮。在你的方法中,你决定毛发的颜色,吠叫的声音,等等。不,你没有在你的方法中加入额外的参数。我犯了一个错误,并将调整答案。您的编辑版本也将是“有效”的,但与拥有一个类的观点不同。假设您希望更新、创建、删除或从用户处进行其他选择,那么您将需要在所有用户中连接到数据库。嗯,连接是可以在构造中设置并在每个方法中使用的东西。@michaeljones它在技术上是正确的,但是,恕我直言,它没有抓住要点-现在
user
类没有保存任何数据,那么你为什么还要拥有它呢?使用这种设计,您不能在应用程序的不同部分之间传递用户,但必须独立地传递变量。
    <?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);
?>