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条)

报告相同问题?

悬赏问题

  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥15 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路
  • ¥15 内网办公电脑进行向日葵
  • ¥15 如何输入双曲线的参数a然后画出双曲线?我输入处理函数加上后就没有用了,不知道怎么回事去掉后双曲线可以画出来
  • ¥50 WPF Lidgren.Network.Core2连接问题
  • ¥15 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术
  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M