First foray into OOP: Correct implementation?

  • (8 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6
  • Last »

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

#46 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2894
  • View blog
  • Posts: 7,544
  • Joined: 08-June 10

Re: First foray into OOP: Correct implementation?

Posted 27 February 2013 - 03:36 AM

then you have either different keys (line break???) or your method has a different code.
Was This Post Helpful? 0
  • +
  • -

#47 Darkranger85  Icon User is offline

  • D.I.C Head

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

Re: First foray into OOP: Correct implementation?

Posted 27 February 2013 - 05:34 AM

Not sure what you mean.
Was This Post Helpful? 0
  • +
  • -

#48 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2894
  • View blog
  • Posts: 7,544
  • Joined: 08-June 10

Re: First foray into OOP: Correct implementation?

Posted 27 February 2013 - 08:09 AM

well, if the index is set (var_dump on session), and your method returns false (var_dump on method), something is very wrong there (you could test var_dump(isset($_SESSION['user_id'])); just for fun).
Was This Post Helpful? 0
  • +
  • -

#49 Darkranger85  Icon User is offline

  • D.I.C Head

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

Re: First foray into OOP: Correct implementation?

Posted 27 February 2013 - 03:40 PM

IT also says boolean false.

I dont understand what could be wrong with such a simple function.
Was This Post Helpful? 0
  • +
  • -

#50 Darkranger85  Icon User is offline

  • D.I.C Head

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

Re: First foray into OOP: Correct implementation?

Posted 27 February 2013 - 04:09 PM

Ok, I might have had a small breakthrough but I'm not sure.

I am now getting an infinite redirect loop.

And I found that it goes away when I comment out the block of code that checks for the user to be active and then logs them out.

So perhaps that block is where the error is and it's destroying the session.

Hmm, after some testing I'm not sure anymore. Cause with that block commented out it seems I can't log out. And var_dumping the session aray on the init script then shows an empty array. . .

I'm really starting to feel overwhelmed.

I'm wondering if there are simply so many things that aren't working right because of my changing from functions to classes that I can't even figure out where the actual issues are coming from.
Was This Post Helpful? 0
  • +
  • -

#51 Darkranger85  Icon User is offline

  • D.I.C Head

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

Re: First foray into OOP: Correct implementation?

Posted 27 February 2013 - 06:22 PM

I'm wondering between two options.

Should I continue debugging this code, or should I completely redo the script again but this time rather than just copying and pasting functions into classes, reworking it one by one.

Or am I jumping the gun with my frustration and giving up on it too soon?
Was This Post Helpful? 0
  • +
  • -

#52 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2894
  • View blog
  • Posts: 7,544
  • Joined: 08-June 10

Re: First foray into OOP: Correct implementation?

Posted 27 February 2013 - 11:11 PM

at this point I recommend to to completely redo it.
Was This Post Helpful? 0
  • +
  • -

#53 Darkranger85  Icon User is offline

  • D.I.C Head

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

Re: First foray into OOP: Correct implementation?

Posted 28 February 2013 - 04:52 PM

Alright

Progress is going well.

The login system and registration system works as expected.

The issue I'm having now is I have two function scripts, I'm trying to call a function from one in the other and it says that it's an undefined variable.

I have the class set in init.php and yet it's not working.

Here's the function, it's the $Check variable that it has a problem with.
 	public static function protectPage(){
 			
 		if(!$Check->loggedIn()){
 			header('Location: protected.php');
			exit();
		}
	}



Yet on init.php I have

global $Check;



I've double checked spellings and such and everything seems fine.
Was This Post Helpful? 0
  • +
  • -

#54 andrewsw  Icon User is online

  • Provides a RESTful service
  • member icon

Reputation: 1080
  • View blog
  • Posts: 3,363
  • Joined: 12-December 12

Re: First foray into OOP: Correct implementation?

Posted 28 February 2013 - 05:05 PM

You shouldn't need to use global, but does init.php instantiate $Check?

global should be used within your function:

	public static function protectPage(){
		global $Check;	
		if(!$Check->loggedIn()){
			header('Location: protected.php');
		exit();
	}
}

Was This Post Helpful? 0
  • +
  • -

#55 Darkranger85  Icon User is offline

  • D.I.C Head

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

Re: First foray into OOP: Correct implementation?

Posted 28 February 2013 - 05:30 PM

No, users.php instantiates it.
Was This Post Helpful? 0
  • +
  • -

#56 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2894
  • View blog
  • Posts: 7,544
  • Joined: 08-June 10

Re: First foray into OOP: Correct implementation?

Posted 28 February 2013 - 10:16 PM

first, forget global. pass variables if you need them.
	public static function protectPage(whatever_intterface_it_is $Check){
		if(!$Check->loggedIn()){
			header('Location: protected.php');
		exit();
	}


second, avoid redirects. you’re inside the script, so you can easily provide the content you want.

though the question remains, why do you have that static method there? it is not related to the class and it uses a foreign object …
Was This Post Helpful? 0
  • +
  • -

#57 Darkranger85  Icon User is offline

  • D.I.C Head

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

Re: First foray into OOP: Correct implementation?

Posted 01 March 2013 - 07:30 AM

Ok, lose the globals, pass the variables. Avoid using the redirect, I'll work on that.

Not so sure what you mean by it not being related. I thought protecting a page was related to security.

As for the foreign object. Are you saying it's bad to use it in the first place?
Was This Post Helpful? 0
  • +
  • -

#58 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2894
  • View blog
  • Posts: 7,544
  • Joined: 08-June 10

Re: First foray into OOP: Correct implementation?

Posted 01 March 2013 - 09:31 AM

View PostDarkranger85, on 01 March 2013 - 03:30 PM, said:

Not so sure what you mean by it not being related. I thought protecting a page was related to security.

depends what you understand by security. I would group it (access to restricted content) under "content control".


View PostDarkranger85, on 01 March 2013 - 03:30 PM, said:

As for the foreign object. Are you saying it's bad to use it in the first place?

let me put it this way: you have a method that doesn’t interact with the class and the only object used in that method needs to be imported.
Was This Post Helpful? 0
  • +
  • -

#59 Darkranger85  Icon User is offline

  • D.I.C Head

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

Re: First foray into OOP: Correct implementation?

Posted 01 March 2013 - 09:47 AM

Ok, I passed the variables and such and got it working. I also replaced the header redirect.

 	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();
		}
	}	



EDIT: So I should make a separate class for one method? And if I make a content control class, isn't it going to have the same issue? Having to call on the method to check the users session.

This post has been edited by Darkranger85: 01 March 2013 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#60 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2894
  • View blog
  • Posts: 7,544
  • Joined: 08-June 10

Re: First foray into OOP: Correct implementation?

Posted 01 March 2013 - 09:55 AM

at this point I usually recommend templates …

one tip for re-useability of objects: don’t echo inside methods, only return. this way you still have the choice what to do with the content.
Was This Post Helpful? 0
  • +
  • -

  • (8 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6
  • Last »