Brian W Posted June 4, 2009 Share Posted June 4, 2009 I've googles and also searched the boards here on phpfreaks and have not found any solutions that seem to fit what I'm going for. I'm new to oop, least in the since of starting from scratch (I've worked with some other apps such as pmWiki). I have 2 classes that I'm working with at the moment; class user and class auth class user is supposed to grab the data of the end user for referencing back to later in the application's execution. class auth is will reference class user for the user's id to use in verifying the user has permission to whatever they are doing. If they don't have permission, I'll be sending them to a login page or giving them an error... basic user system. So far, I've gotten as far as getting the very basics of class user working, easy. I want to get the value of $user->id into class auth during the construction of class auth rather than inputting it through some method. Here is what I have include.php: <?php error_reporting(E_ALL); if(!isset($_SESSION)){ session_start(); } $_SESSION['id'] = 1;//testing include('../../Connections/Epm.php'); //Classes include('user.php'); include('auth.php'); include('projects.php'); include('clients.php'); if(true){ // for testing $epm->user = new user; $epm->auth = new auth; } ?> user.php: <?php if(!isset($_SESSION)){ session_start(); } class user { function user(){ $this->id = $_SESSION['id']; if($this->id <= 0){ $this->login(); } else { $query = mysql_query("SELECT * FROM bexusers WHERE ID =".$this->id) or die("Error: Could not collect data"); $user = mysql_fetch_assoc($query); $this->email = $user['email']; $this->first_name = $user['first_name']; $this->last_name = $user['last_name']; $this->address = trim($user['first_name']." ".$user['last_name']." <".$user['email'].">"); } } function login(){ $_SESSION['ref'] = $_SERVER['PHP_SELF']; header('location: login.php'); die("Please Sign In"); } function return_info(){ return array("id"=>$this->id, "first_name"=>$this->first_name, "last_name"=>$this->last_name, "email"=>$this->email); } } ?> auth.php: <?php class auth{ function auth(){ if($epm->user->id <= 0){ die('Error: no user id found'); //will send them to sign in screen later... } } function check_project($id){ $sql = sprintf("SELECT p.* FROM projects p LEFT JOIN usergroup");//incomplete query! } } ?> the error I get is: PHP Notice: Undefined variable: epm in E:\wwwroot\bexexpress\EPM v3\Classes\auth.php on line 5 PHP Notice: Trying to get property of non-object in E:\wwwroot\bexexpress\EPM v3\Classes\auth.php on line 5 PHP Notice: Trying to get property of non-object in E:\wwwroot\bexexpress\EPM v3\Classes\auth.php on line 5 I understand why it doesn't work, the variable $epm isn't an object w/in the class auth, but I can't seem to figure out how to get the desired effect. Thanks for any input, suggestions, slaps to the back of the head ... Quote Link to comment Share on other sites More sharing options...
gevans Posted June 4, 2009 Share Posted June 4, 2009 Why not pass the user id on instantiation of the auth object; <?php $epm->user = new user; $epm->auth = new auth($epm->user->id); <?php class auth{ function auth($user_id){ if($user_id <= 0){ die('Error: no user id found'); //will send them to sign in screen later... } } Quote Link to comment Share on other sites More sharing options...
Brian W Posted June 4, 2009 Author Share Posted June 4, 2009 that is a quick fix for this issue, but this app is going to get quite deep with several classes referencing eachother repeatedly and hopefully using eachother's methods for "auth checks" and for figuring out what to show the end user. irreverent data about the app. below (only read if your bored ;-) Not that this is a new idea, but the app is a version 3 of a project manager I developed for my employer. Version 1 blew, version 2 works but isn't OOP and is a bit limited in functionality (plus has some bad coding style). User will sign in, setting the $_SESSION['id'], and depending on their permissions will get to view all clients or just the client they are a member of. Under clients is projects, under projects is tasks. Basic management system so far. In v3, I'm adding a PC tracking system for the hardware department to keep tabs on computer's history and whats in em. We now have some contract workers that we want to tighten the security on, like not showing them anything that they don't absolutely need to see, which is how I justified a rewrite of the app since previously security (as to who can see what) was never an issue and never requested. Quote Link to comment Share on other sites More sharing options...
Brian W Posted June 4, 2009 Author Share Posted June 4, 2009 I've made no progress in the past hour messing with it more. in the OOP vs not OOP battle for php, I think OOP looses out in my book unless someone can help me to understand how to get the classes to use variables and methods from other classes. Quote Link to comment Share on other sites More sharing options...
Philip Posted June 4, 2009 Share Posted June 4, 2009 You're out of scope to reach $epm. You need to pass it via a construct. My suggestion: If you're using PHP5, you can use type hinting. However, based on your current setup it looks like you either aren't using PHP5's OOP to its fullest, or you're using PHP4. Type hinting based on your code: <?php // PHP link: // http://www.php.net/oop5.typehinting // USER CLASS class user { function __construct(){ $this->id = $_SESSION['id']; if($this->id <= 0){ $this->login(); } else { $query = mysql_query("SELECT * FROM bexusers WHERE ID =".$this->id) or die("Error: Could not collect data"); $user = mysql_fetch_assoc($query); $this->email = $user['email']; $this->first_name = $user['first_name']; $this->last_name = $user['last_name']; $this->address = trim($user['first_name']." ".$user['last_name']." <".$user['email'].">"); } } function login(){ $_SESSION['ref'] = $_SERVER['PHP_SELF']; header('location: login.php'); die("Please Sign In"); } function return_info(){ return array("id"=>$this->id, "first_name"=>$this->first_name, "last_name"=>$this->last_name, "email"=>$this->email); } } // AUTH CLASS class auth{ function __construct(user $user_class){ if($user_class->user->id <= 0){ die('Error: no user id found'); //will send them to sign in screen later... } } function check_project($id){ $sql = sprintf("SELECT p.* FROM projects p LEFT JOIN usergroup");//incomplete query! } } // From include.php: if(true){ // for testing $epm->user = new user; $epm->auth = new auth($epm->user); } ?> Quote Link to comment Share on other sites More sharing options...
Brian W Posted June 4, 2009 Author Share Posted June 4, 2009 Hey, that makes since... So basically I pass the already created object to the next class and then to the next class and then to the next class... simply stacking them. Problem though, the variables become static... if(true){ // for testing $epm->user = new user; $epm->auth = new auth($epm->user); $epm->id = 2; echo $epm->auth->id; returns 1, not 2. I think I'm going to take a different rout on this and just pass the variables around outside the classes themselves. Not as pretty, doesn't seem right to me, but it will work. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.