1
#######################################
#### THIS ISSUE HAS BEEN RESOLVED! ####
########################################
The issue is that the object lost its scope since I have a separate function
that puts together all the template files in order and the class wasn't
insantiated within that function. I think what I'm going to try to do is
rewrite the uFlex auth class as separate functions so it doesn't lose
scope within my framework.

我正在将 uFlex 身份验证类集成到我的 PHP 框架中。我对类本身有深刻的理解,并且已经让所有示例都可以自己工作,只剩下它们的依赖关系。我已经能够让它在我的框架内工作,但只有在我需要对它做任何事情之前重新实例化该对象,这是重复且不必要的。

在我的框架中,我有一个 init/bootstrap 文件,其中包含首先要使用的所有必要文件,在这种情况下,类 uflex.php 文件包含在此处。然后我为 uflex 对象建立正确的数据库凭据并启动 uflex 对象。然后在包含这些文件并实例化 uflex 对象后,我会根据请求的 url 以特定顺序包含模板文件。

对于登录页面示例: http: //mysite.com/login/

<?php //files included when above url is requested
include 'framework-init.php';
include 'process-login-form.php';
include 'login-page.php';
?>

文件:framework-init.php

<?php
//framework code here
include 'path/to/framework-code.php';

//uFlex code here
include 'path/to/uflex.php';
$user = new uFlex(false);
$user->db['host'] = 'dbhost';
$user->db['user'] = 'dbuser';
$user->db['pass'] = 'dbpass';
$user->db['name'] = 'dbname';
$user->start();
?>

文件:进程登录form.php

<?php
if ($_POST['action'] == 'login') {
    $user->login($_POST['username'], $_POST['password'], $_POST['auto']);
    if ($user->signed) { header('Location: http://mysite.com/dashboard/'); }
    else {
        //handle error logging in here
    }
}
?>

文件:登录页面.php

<form method="post" action="">
<input type="hidden" name="action" value="login">
<label>Username:</label>
<input type="text" name="username" />
<br>
<label>Password:</label>
<input type="password" name="password" />
<br>
<label>Remember me?:</label>
<input type="checkbox" name="auto" />
<br>

<input type="submit" value="login" />
</form>

这是错误:

Fatal error: Call to a member function login() on a non-object in /path/to/login.process.php on line 18

错误发生在文件 process-login-form.php 中的这一行

$user->login($_POST['username'], $_POST['password'], $_POST['auto']);

这里发生了什么?页面加载正常,直到我提交表单,然后它返回上面的错误。

但是,如果我的 process-login-form.php 文件如下所示,这不会引发错误:

<?php
// no need to include the uflex.php file here...
// it was already included with the framework-init.php file
$user = new uFlex(false);
$user->db['host'] = 'dbhost';
$user->db['user'] = 'dbuser';
$user->db['pass'] = 'dbpass';
$user->db['name'] = 'dbname';
$user->start();
if ($_POST['action'] == 'login') {
    $user->login($_POST['username'], $_POST['password'], $_POST['auto']);
    if ($user->signed) { header('Location: http://mysite.com/dashboard/'; }
    else {
        //handle error logging in here
    }
}
?>

为什么我必须在 process-login-form.php 文件中重新实例化它已经在 framework-init.php 文件中实例化的对象?这只是我对 OOP 不了解的事情吗?当使用“post”方法提交表单时,我看不到的东西是否发生了变化?

4

0 回答 0