【问题标题】:Undefined variable in OOP PHPOOP PHP中的未定义变量
【发布时间】:2015-11-17 07:16:26
【问题描述】:

我正在尝试过渡到 OOP PHP,以帮助清理我目前正在使用的代码集群。

我正在使用 PHPPass 在我的数据库中对密码进行哈希处理,但使用这种 OOP 方法,我无法理解如何在我的班级的登录函数中调用它。

据我所知,在我尝试调用它的所有地方,它总是在我的类初始化之前声明,但它仍然告诉我它是未定义的或非对象。

db_config.php

...
require_once("PasswordHash.php"); // Location no.1 to try it
$password_hash = new PasswordHash(8, FALSE);

include_once("DB.php");
$db = new DB($db_connection);

...

init.php

//require_once("PasswordHash.php"); // Location no.2 to try it
//$password_hash = new PasswordHash(8, FALSE);

require_once("db_config.php")

..Other init stuff..

DB.php

class DB {
   ... 
   public function login() {
      // global $password_hash; -> if uncommented I get an error saying it's a non-object

      // Error here 
      $password_accepted = $password_hash->CheckPassword($p, $hp);
   } 
   ...
}

登录.php

require_once("init.php");

$db->login();

我还没有完全理解类作用域在 PHP 中是如何工作的,所以我觉得我错过了一些东西。

【问题讨论】:

  • PasswordHash 不是基于您在此处显示的类的名称。
  • 我的错,那个类肯定存在

标签: php oop scope


【解决方案1】:

您需要将哈希传递给该类,因为该类只有一个内部范围。

$formData = array();
$formData['email'] = 'user@email.com';

require_once("PasswordHash.php"); // Location no.1 to try it
$formData['password_hash'] = new PasswordHash(8, FALSE);

include_once("DB.php");
$db = new DB($db_connection, $formData);

在 DB.php 中:

class DB {
  // Stores the user input form data for use within the class?
  private $formData;
  // Runs when the class is constructed
  public function __construct($formData)
  {
    // When the class is constructed then store this for local/interal use
    $this->$formData = $formData;
  }
  public function login() {
    // The boolean result of of checking of an internal method
    // that compares user credentials against the database information?
    $password_accepted = $this->CheckPassword(
      $this->formData['email'], 
      $this->formData['password_hash']
    );
  }
  private function CheckPassword($email, $pass) {
    // Do query and bind in $user and $pass
    // Return true if everthing passes
  }
}

编辑:我夸大了将变量传递到类和方法中的用法,以帮助您了解这方面的问题,但您也可以执行以下操作:

    ...
    $password_accepted = $this->CheckPassword();
  }
  private function CheckPassword() {
    // Do query and bind in $this->formData['email'] and $this->formData['password_hash']
    // Return true if everthing passes
  }
}

【讨论】:

    【解决方案2】:

    只需 inject 哈希实例,就像您对 db_connection 所做的那样。

    class DB {
        ...
        public function __construct($db_connection, $password_hash) {
            // you probably already have something like
            $this->connection = $db_connection;
            // now do the same for the hash object
            $this->pwhash = $password_hash;
        }
    
        public function login() {
        ...  
            $password_accepted = $this->pwhash->CheckPassword($p, $hp);
            ...
        }
    }
    

    (稍微偏离主题:数据库和哈希......以及密码、电子邮件和缓冲区这些是我最不喜欢初学者摆弄的课程)

    【讨论】:

    • jinx,你欠我一杯啤酒 :)
    • 为了避免这种情况,我添加了一个依赖注入文章的链接 :)
    • 我在采用程序方法时都没有遇到太多麻烦,只是我以前从未接触过的 PHP 的 OOP 方面
    • 谢谢大家,我一直想研究 OAuth 一段时间
    猜你喜欢
    • 2019-09-18
    • 2015-10-13
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    相关资源
    最近更新 更多