在非对象上调用成员函数login()(uFlex错误)

时间:2013-10-30 20:08:39

标签: php class oop object member-functions

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

文件:process-login-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
    }
}
?>

文件:login-page.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”方法提交表单时,我没有看到某些变化吗?

0 个答案:

没有答案