First foray into OOP: Correct implementation?

  • (8 Pages)
  • +
  • « First
  • 6
  • 7
  • 8

113 Replies - 8613 Views - Last Post: 23 March 2013 - 11:52 PM Rate Topic: -----

#106 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 31-August 12

Re: First foray into OOP: Correct implementation?

Posted 20 March 2013 - 01:31 PM

Hmm, well that stinks. I'll give it another go but I'm quickly running out of ideas of how to fix this one. And no one else seems to know either.
Was This Post Helpful? 0
  • +
  • -

#107 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 31-August 12

Re: First foray into OOP: Correct implementation?

Posted 21 March 2013 - 11:16 AM

Alright.

I have error reporting on and I've print_r and var_dumped every variable I can find all the way back up the chain and I have no clue where the problem is.

It all comes back to how can a variable that contains an object be undefined and be a non-object?

Considering my if statement for deciding whether or not to display a login form or a user profile works fine, theoretically my loggedIn method must work. So, I cant figure out why it would then tell me there is a problem with it when I try to access a protected page.
Was This Post Helpful? 0
  • +
  • -

#108 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: First foray into OOP: Correct implementation?

Posted 21 March 2013 - 02:26 PM

if we don’t see the code (the places where you dumped the variables) and the according dumps, we cannot say anything either.
Was This Post Helpful? 0
  • +
  • -

#109 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 31-August 12

Re: First foray into OOP: Correct implementation?

Posted 21 March 2013 - 03:03 PM

Well, the primary var_dump is this one:

			<?php
				var_dump($Check);
				if ($Check->loggedIn()){
					include 'includes/modules/logged_in.mod.php';
				} else{
					include 'includes/modules/login.mod.php';
				}
			?>



Which returns:

object(check)[3]
  protected 'pdo' => 
    object(PDO)[2



Following that code back the the check->loggedIn() method:

		public function loggedIn(){
			return (isset($_SESSION['user_id']));
		}



Not really anything there to test, but for the sake of not missing anything I var_dumped it and it contains the correct user_id.

Now, going to a protected page that causes the error on navbar.inc.php I var dump the variable that is being called :

	include 'core/init.php';
	var_dump($Content);
	$Content->protectPage();



Which returns:

object(content)[9]
  protected 'Check' => 
    object(check)[3]
      protected 'pdo' => 



Which is what I expect to find in that variable.

Following that method back:

	public function protectPage(){
 		
 		if(!$this->Check->loggedIn()){
 			include '/includes/overall/header_main.php';
			echo '<div class="msg_module">';
			echo '<h2>Protected Page:</h2>';
			echo 'The page you are trying to access requires you to login.<br /><br />';
			echo '<a href="index.php">Login</a>';
			echo '</div>';
			include '/includes/overall/footer_main.php';
			exit();
		}
	}



Granted, this method has to be fixed as to not echo out the page directly, but other than that I don't see anything to check on.

My last effort was to check the instantiation of these classes right after their being declared which gave me these results:

//var_dump of $Check
object(check)[3]
  protected 'pdo' => 
    object(PDO)[2]

//var_dump of $Content
object(content)[9]
  protected 'Check' => 
    object(check)[3]
      protected 'pdo' => 
        object(PDO)[2]



So that is what I did. I worked my way backwards through the code and I don't see any undefined variables or anything. I hope that I was thorough in my debugging method at least lol.
Was This Post Helpful? 0
  • +
  • -

#110 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 31-August 12

Re: First foray into OOP: Correct implementation?

Posted 22 March 2013 - 09:44 AM

Alright, I changed the methods to check for the user to be loggedin directly, without calling the $Check method at all like so:

 		if(!isset($_SESSION['user_id'])){
 			include '/includes/overall/header_main.php';
			echo '<div class="msg_module">';
			echo '<h2>Protected Page:</h2>';
			echo 'The page you are trying to access requires you to login.<br /><br />';
			echo '<a href="index.php">Login</a>';
			echo '</div>';
			include '/includes/overall/footer_main.php';
			exit();
		}
	}



It's still giving the error on the navbar. So that, as far as I can see, must mean that the problem is with the code in the navbar or the methods it calls.

But I still don't see why the if statement works fine by itself and only is seen as an error when accessing a protected page. Especially now that the method for protecting the page doesn't even call a method now.

Ok, new development. I changed the following code to now include the $this variable:

			<?php
				if ($this->Check->loggedIn()){
					include 'includes/modules/logged_in.mod.php';
				} else{
					include 'includes/modules/login.mod.php';
				}
			?>



Now the result is that I see the protected page message, which I didn't see before. But it's protesting my use of the $this variable:

Fatal error: Using $this when not in object context in C:\wamp\www\Projects\Planet Ruin v4\includes\navbar.inc.php on line 8

However, I'm also seeing it repeat my navbar. So I'm wondering if the problem comes from the fact that in the protected methods the webpage template is included around the message so therefore so is the init.php.

Perhaps thats somehow messing with the code? And so if I change the method to return an error message to be displayed instead of directly displaying the error message maybe it will clear up the problem.

Talking out loud kind of, I'll work on that later and post back my results. Have to drive to Connecticut in a few hours so not much time to do it now.

This post has been edited by Darkranger85: 22 March 2013 - 10:50 AM

Was This Post Helpful? 0
  • +
  • -

#111 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 31-August 12

Re: First foray into OOP: Correct implementation?

Posted 22 March 2013 - 11:07 AM

Alright, I have rewritten my page protection method and so far everything is working. Let me know what you think of the new implementation. :)

Here is the method itself:

	public function protectPage(){
 		
 		if(!$this->Check->loggedIn()){
 			$msg = '<div class="msg_module">';
			$msg .= '<h2>Protected Page:</h2>';
			$msg .= 'The page you are trying to access requires you to login.<br /><br />';
			$msg .= '<a href="index.php">Login</a>';
			$msg .= '</div>';
			return $msg;
			
			/*
			echo '<div class="msg_module">';
			echo '<h2>Protected Page:</h2>';
			echo 'The page you are trying to access requires you to login.<br /><br />';
			echo '<a href="index.php">Login</a>';
			echo '</div>';
			 */
			exit();
		}
	}



And here is how I have it on the top of a protected page:

<?php 
	include 'core/init.php';
	$access = $Content->protectPage();
	
	if(isset($access)){
		include 'includes/overall/header_main.php';
		echo $access;
		include 'includes/overall/footer_main.php';
		exit();
	}


Was This Post Helpful? 0
  • +
  • -

#112 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: First foray into OOP: Correct implementation?

Posted 22 March 2013 - 06:29 PM

code block one: the exit() will never be called (located after (!) a return statement).
Was This Post Helpful? 0
  • +
  • -

#113 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 31-August 12

Re: First foray into OOP: Correct implementation?

Posted 23 March 2013 - 08:14 PM

Yeah, I noticed that too.

I got rid of it.

Other than that though, does it seem like a sound implimentation?
Was This Post Helpful? 0
  • +
  • -

#114 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: First foray into OOP: Correct implementation?

Posted 23 March 2013 - 11:52 PM

let me put it this way: I’d get the HTML from someplace else. other than that there’s too less code to say something about proper implementation.
Was This Post Helpful? 0
  • +
  • -

  • (8 Pages)
  • +
  • « First
  • 6
  • 7
  • 8