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 18The 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?
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/uflex/00fe41e9-ef39-4944-b6e2-57a885cce63c%40googlegroups.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.
--