Call to a member function login() on a non-object

78 views
Skip to first unread message

Robot Sushi

unread,
Oct 30, 2013, 9:21:33 PM10/30/13
to uf...@googlegroups.com

I'm integrating the uFlex authentication class into my PHP framework. I have a solid understanding of the class itself and have gotten all examples to work on their own stripped down to their bare dependencies. I have been able to get it to work within my framework but only if I reinsantiate the object just before I need to do anything with it, which is repetitive and unnecessary.

In my framework, I have an init/bootstrap file that includes all the necessary files to utilize first, in this case the class uflex.php file is included here. Then I establish the correct database credentials for the uflex object and start the uflex object. Then immediately after those files are included and the uflex object is insantiated, I include template files in a specific order based on the url requested.

For the login page example: 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';
?>


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


File: 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
    }
}
?>


File: 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>


This is the error:

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


The error occurs in this line here in the file process-login-form.php

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


What's going on here? The page loads fine until I submit the form, then it returns the error above.

However, this does not throw an error if my process-login-form.php file looks like this:

<?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
    }
}
?>


Why do I have to reinsantiate the object in the process-login-form.php file it's already been insantiated in the framework-init.php file? Is this just something I don't get about OOP? Does something change that I'm not seeing when a form is submitted using the "post" method?

Simon ilett

unread,
Oct 30, 2013, 9:44:31 PM10/30/13
to uf...@googlegroups.com

Sounds like you have lost the scope in which $user is first created.

If you are using include after include and defining objects like $user they are basically like globals.

But if you are at some point  calling a function to figure out which files to include, that function will know about $user,  but the include it adds wont! as the include inside of the function is now bound to that functions scope.

Id say that is your issue. If you are using nested includes like that you may need to define $user as a PHP global.

--
Project's home page http://ptejada.com/projects/uFlex/
---
You received this message because you are subscribed to the Google Groups "uFlex" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uflex+un...@googlegroups.com.
To post to this group, send email to uf...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/uflex/e17312ff-c1f4-480f-b1f3-d1acd5883712%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Robot Sushi

unread,
Oct 30, 2013, 10:04:10 PM10/30/13
to uf...@googlegroups.com
OK great that makes a lot of sense thank you so much for the quick reply!

If that's the case, then how would I define $user global? I've read this is generally not good practice but I'd at least like to try it to see what it is worth. This also leaves me thinking I need to redesign my framework in some way to accommodate these types of things I will use in the future.

Robot Sushi

unread,
Oct 30, 2013, 10:15:55 PM10/30/13
to uf...@googlegroups.com
With your insight on the object losing its scope I was actually able to resolve the issue by insantiating the object within the function that puts together the correct template files. Although I do not like this implimentation, it does work. I'd like to see if i can rewrite the uflex class into a set of functions so as long as the files were included before the functions are called, they would not lose scope/functionality. Any suggestions on this approach?

Simon ilett

unread,
Oct 30, 2013, 10:19:29 PM10/30/13
to uf...@googlegroups.com
There are many ways to do what you want, I think best your read this though



Secondly you may be able to get an idea of how one would correctly scope the user object via my own framework that uses uFlex. Specifically this file



Full framework at 


rgds simon



For more options, visit https://groups.google.com/groups/opt_out.



--
rgds simon@

Pablo Tejada

unread,
Oct 30, 2013, 10:21:29 PM10/30/13
to uf...@googlegroups.com

Hi,
I have not confirm/test it yet but i think you can not share variables across include file, is a PHP thing. An included script may access the parent script, the one including it, but not variables from other parallel included script.

To test my theory try this instead, instantiate the uFlex class right after the your framework-init inclusion the $user variable should then be available on the included process-login script.

Let me was the outcome.

Now here is my unsolicited opinion, from the code you have posted and the issue at hand i find your framework to be inefficient. My tip for you is to research the topic of autoloading PHP scripts/classes and the MVC paradigm.

--
Reply all
Reply to author
Forward
0 new messages