serien Posted August 16, 2012 Share Posted August 16, 2012 I am writing a webpage for a friend and it involves forums. I have been working on switching the entire site into object oriented to better organize it (the straight php version was a bit too messy for me) I have been slowly working through the kinks, but I finally hit a snag that is broad enough to stump google and beyond my knowledge to hash out. Any help you guys can give will be greatly appreciated!! I have an overall master class called 'Site'. it holds the logged in user's session as well as database information. Here is the code for that. I am only including the constructor because the other functions are not involved in my current issue. class Site { // ====================================== Properties ==================== public $loggeduser; // User Class public $baseurl = '/dellusionary'; //======================================== Methods ========================= /* Constructor */ public function __construct() { $this->mysql_connect(); session_start(); $this->loggeduser = (!isset($_SESSION['userid'])) ? NULL : new User($_SESSION['userid']); } } Now, I want to be able to use this "loggeduser" object in my Board class in the forums. I wish to verify whether the current user has the correct level to view the forum (is staff vs is member). Here is that class class Board extends Site { // ====================================== Properties ==================== public $board_normaltopics; // array of normal Topics within Board public $board_stickytopics; // array of important, stickied Topics within Board public $board_subboards; // array of sub-Boards within Board public $board_id; public $board_category; // Category Class public $board_accesslevel; public $board_parent; // Board Class or NULL public $board_description; //======================================== Methods ========================= /* Constructor */ public function Board($id) { // Set Topics $topicquery = mysql_query("SELECT * FROM forum__topic WHERE topicboard = $id ORDER BY topicdate DESC"); while ($topic = mysql_fetch_array($topicquery)) { $this->board_topics[] = new Topic($topic['topicid']); } // Set Board Information $boardarray = mysql_fetch_array(mysql_query("SELECT * FROM forum__board WHERE boardid = $id")); $this->board_id = $id; $this->board_name = $boardarray['boardname']; //$this->board_category = new Category ($boardarray['boardcat']); $this->board_accesslevel = $boardarray['boardaccesslevel']; $this->board_parent = ($boardarray['boardparent'] == 0) ? NULL : new Board($boardarray['boardparent']); $this->board_description = $boardarray['boarddesc']; } /*******************/ /* Operation Functions */ public function valid_board() { $query=mysql_query("SELECT * FROM forum__board WHERE boardid = $this->board_id"); $num= mysql_num_rows($query); if ($num != 0) { return (($this->board_accesslevel) <= ($this->loggeduser->userlevel)); // LINE 133 } else { return false; } } } Again, only included problem functions. Is this possible? I keep getting the following error: Trying to get property of non-object in C:\wamp\www\dellusionary\classes\forum_class.php on line 133 I marked the line above Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/267196-in-need-of-some-object-oriented-assistance/ Share on other sites More sharing options...
scootstah Posted August 16, 2012 Share Posted August 16, 2012 You didn't provide the code for your User class, so we don't know what the $this->loggeduser holds. $this->loggeduser = (!isset($_SESSION['userid'])) ? NULL : new User($_SESSION['userid']); Here, you are assigning $this->loggeduser to NULL if the $_SESSION['userid'] is unset. So, in that case, $this->loggeduser->userlevel will be undefined (and thus the error you're getting). What you should do instead is return an empty object with empty values from the User class, or check that userlevel exists first. Quote Link to comment https://forums.phpfreaks.com/topic/267196-in-need-of-some-object-oriented-assistance/#findComment-1370023 Share on other sites More sharing options...
requinix Posted August 16, 2012 Share Posted August 16, 2012 Also, Board's constructor needs to call the parent constructor too - that doesn't happen automatically. Also, please call it "__construct". public function __construct($id) { // in Board parent::__construct(); // in Site Quote Link to comment https://forums.phpfreaks.com/topic/267196-in-need-of-some-object-oriented-assistance/#findComment-1370037 Share on other sites More sharing options...
serien Posted August 17, 2012 Author Share Posted August 17, 2012 thanks! yeah, I thought about the "NULL" thing also, but the top of each page has a check. If $site->loggeduser = NULL, then it redirects back to the home page. the page where the error lies shouldn't be visible to viewers who aren't logged in on a session. here is the code for the Users class class User extends Site { // ====================================== Properties ==================== public $userid; // User's id public $username; // User's display name public $bio; // User's Profile Bio public $userdate; // User's join date public $userrank; // User's rank title public $userlevel; // User's access level public $staffbio; // If user is staff, the bio that displays public $template; // User's layout of choice public $online; // User's online status public $rankid; public $sitecash; public $realcash; public $activepet; // User's active pet, possibly move... private $loginname; //User's login private $password; // User's password //======================================== Methods ========================= public function User($id) { $this->userid = $id; $userinfo = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE userid = '$id'")); $this->username = $userinfo['displayname']; $this->bio = $userinfo['userbio']; $this->userdate = date("F j, Y", strtotime($userinfo['joindate'])); $this->template = $userinfo['template']; $this->sitecash = $userinfo['bits']; $this->realcash = $userinfo['bobs']; $this->activepet = $userinfo['activepet']; $this->online = ($userinfo['online'] == 1); $this->rankid = $userinfo['rank']; $rankinfo = mysql_fetch_array(mysql_query("SELECT * FROM ranks WHERE rankid = '".$userinfo['rank']."'")); $this->userrank = $rankinfo['ranktitle']; $this->userlevel = $rankinfo['ranklevel']; $this->staffbio = $userinfo['staffbio']; } } I will try the construct thing! see if it works... I was between using it and the function call, it makes sense that it would be the better option. I have never seen the parent::construct call though, that is very good to know! EDIT: The parent::__construct(); did the trick!!!!!! Thank you sooo much!!! On to different errors now =D I will still go back and change all of the constructors though, since it seems to be better programming practice. Awesome! Quote Link to comment https://forums.phpfreaks.com/topic/267196-in-need-of-some-object-oriented-assistance/#findComment-1370044 Share on other sites More sharing options...
scootstah Posted August 17, 2012 Share Posted August 17, 2012 Also, please call it "__construct". Yeah, I forgot to mention that. Using the class name as the constructor is oldschool PHP4 stuff. PHP5 supports __construct, so use that instead. Quote Link to comment https://forums.phpfreaks.com/topic/267196-in-need-of-some-object-oriented-assistance/#findComment-1370051 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.