doujia4619 2015-08-23 23:32 采纳率: 0%
浏览 45
已采纳

OOP PHP中的未定义变量

I'm trying to make the transition to OOP PHP to help clean up the cluster of code I'm currently working with.

I'm using PHPass to hash passwords in my database but with this OOP approach, I can't get my head around how to call it in my class' login function.

As far as I can see, in all the places I've tried calling it, it's always declared before my class is initialised but it's still telling my it's undefined or a non-object.

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);
   } 
   ...
}

login.php

require_once("init.php");

$db->login();

I still haven't got my head fully around how class scope works in PHP so I have a feeling I'm missing something.

  • 写回答

2条回答 默认 最新

  • dqrdlqpo775594 2015-08-23 23:40
    关注

    You need to pass the hash into the class as the class has only an internal scope.

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

    and in 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
      }
    }
    

    Edit: I exaggerated the use of passing the variables into classes and methods to help you to wrap your head around this aspect of things but you could also do something like:

        ...
        $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
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择