First foray into OOP: Correct implementation?

  • (8 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »

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

#16 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 25 February 2013 - 07:41 AM

Jack is correct, I do need to learn how to decipher my error messages better.

This one:

Argument 1 passed to userActions::__CONSTRUCT() must be an instance of PDO, none given

Confused me because I thought it was referring to the construct method and so I thought there was something wrong with what I put in its parameters.

In hind sight, it's obvious. The error says that it's referring t the arguments "passed" to the construct. I should have realized that. I let myself be intimidated because of how new I am to classes.

I do apologize, I don't want you guys to think that I'm trying to take advantage of your time and help.

I promise I'll try harder from now on. And feel free to make me work for my answer. :-)

I really do appreciate you guys and your help!

EDIT: And dor, I promise I am going through and changing my casing.

This post has been edited by Darkranger85: 25 February 2013 - 07:44 AM

Was This Post Helpful? 1
  • +
  • -

#17 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

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

Re: First foray into OOP: Correct implementation?

Posted 25 February 2013 - 07:59 AM

View PostDarkranger85, on 25 February 2013 - 03:41 PM, said:

In hind sight, it's obvious. The error says that it's referring t the arguments "passed" to the construct. I should have realized that. I let myself be intimidated because of how new I am to classes.

actually, youíre the first one to admit that. so I think youíre on a good way.
Was This Post Helpful? 0
  • +
  • -

#18 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 25 February 2013 - 08:31 AM

Thanks! :)

You guys are awesome!

Now I'm running into an issue, no error messages. Just something that's not working quite the way I expected. (And you can make me work for it.)

On my registration page I have an elseif block that looks for an array called $errors to be empty. If it's not empty it prints out an html/css bock with all the errors that need to be fixed.

It does indeed print out the errors just like I want. However, when there are no errors, it still shows the box and heading.

I found that this is because the $errors array contains an empty array, but I have no idea where the array came from.

The $errors array is used 8 or 9 times on the page, and each time it's contained in an if statement like this:

if (error occurs){
    $error[] = 'error message';
}



Now, here is the elseif block:

	} elseif (!empty($errors)){
		echo '<div class="msg_module">';
		echo '<h2>Registration Errors:</h2>';
		echo $General->outputErrors($errors);
		echo '</div>';
	}



And here is the method that it calls:

public function outputErrors($errors = ''){
			
			if(is_array($errors)){
				// handle for passed array
				foreach ($errors as $error){
					if(is_array($error)){
						general::outputErrors($error);
					} else{
					general::$errStr.= ($error != '')?'<li>'.$error.'</li>':'';
					}
				}
			} else if($errors != ''){
			// handle for passed string
			general::$errStr = $errors;
			}
		return '<ul id="error_list">'.general::$errStr.'</ul>';
	 }//outputErrors



$errStr is declared in the class as a static property.

The errors and such worked as expected before I moved things into classes. So it must be something that works differently because of the class or whatever. I'm happy to work it out but at the moment I don't know where to look.
Was This Post Helpful? 0
  • +
  • -

#19 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

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

Re: First foray into OOP: Correct implementation?

Posted 25 February 2013 - 08:39 AM

on line #6 you test for an array inside the $errors array. there must be a reason for that and that reason is probably around when you get that empty array.
Was This Post Helpful? 0
  • +
  • -

#20 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 25 February 2013 - 08:41 AM

Yeah, I implemented that to try and counteract the array.

I'm still not sure where it's coming from. Are you saying that it's part of the problem?
Was This Post Helpful? 0
  • +
  • -

#21 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

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

Re: First foray into OOP: Correct implementation?

Posted 25 February 2013 - 08:48 AM

that code only makes sense when you *know* that somewhere in your code an array is inserted. hence there must be a place where that is done and you "only" have to find that.

personally I think such global error arrays are not the right way to deal with errors in OOP. and it clearly shows the reason why most people call global variable bad.
Iím a fan of Exceptions and I also do not see the need to capture *all* occurring errors. if there is an error (that you canít handle), you need to quit the script (in an orderly manner, of course), period.
Was This Post Helpful? 0
  • +
  • -

#22 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 25 February 2013 - 08:58 AM

Yeah, obviously before this I was writing this script in more or less a procedural way.

In my ignorance, I thought that I could switch the functions into classes and just change how the script calls them to reflect the added class and BAM it would work. . . I was wrong lol.

Turns out my code is probably some strange hybrid of procedural/OOP techniques.

I didn't know that I should use exceptions and exiting the whole script in form validation. I thought you just colloect the errors to show to the user to be fixed.

Do you think that I should scrap the way I'm handling the validation and errors in my forms for a more efficient approach? If so, do you have or know a guide that I should follow to get my feet wet?

Cause if I'm using bad practice I don't want that to become a habit.

This post has been edited by Darkranger85: 25 February 2013 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#23 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

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

Re: First foray into OOP: Correct implementation?

Posted 25 February 2013 - 09:04 AM

even my code is a procedural/OOP hybrid ...

Quote

Do you think that I should scrap the way I'm handling the validation and errors in my forms for a more efficient approach?

I do not know how you validate your form in the first place.

I also think that the biggest problem in OOP is figuring out, what using objects means.

and I guess without some kind of framework, it is hard to get effectiveness in OOP.
Was This Post Helpful? 0
  • +
  • -

#24 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 25 February 2013 - 09:07 AM

Hmm,

So should I not be using OOP in the first place perhaps?

Here is a basic snippet of how my validation is done, it's from the script that is giving my the problem mentioned above:

if ($Check->userExists($_POST['username'])){
				$errors[] = 'That username is already taken. Please try another.';
			}
			if (preg_match('/\\s/', $_POST['username'])){
				$errors[] = 'Username may not contain spaces.';
			}
			if (strlen($_POST['password']) < 6){
				$errors[] = 'Password must have at least 6 characters.';
			}
			if ($_POST['password'] !== $_POST['password2']){
				$errors[] = 'Passwords do not match.';
			}
			if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
				$errors[] = 'Email is not valid.';
			}
			if ($Check->emailExists($_POST['password'])){
				$errors[] = 'Email is already in use. Have you registered before?';
			}
			if ($Check->empireExists($_POST['empire_name'])){
				$errors[] = 'Empire name is already in use.'; 
			}
			if (!empty($_POST['botcheck'])){
				if ($_POST['botcheck'] != 10){
					$errors[] = 'Bot check was answered incorrectly';
				}
			} else{
				$errors[] = 'Bot check must be answered.';
			}


Was This Post Helpful? 0
  • +
  • -

#25 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

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

Re: First foray into OOP: Correct implementation?

Posted 25 February 2013 - 09:16 AM

View PostDarkranger85, on 25 February 2013 - 05:07 PM, said:

So should I not be using OOP in the first place perhaps?

thereís nothing wrong with using OOP. but you may get as many opinions as people you ask.

if you like you could even try the functional approach to programming (though PHP is not suited for that).
Was This Post Helpful? 0
  • +
  • -

#26 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 25 February 2013 - 09:23 AM

Hmm, at this point I'm a little conflicted about what I should be doing.

My code was working pretty much as I expected and I decided to go to OOP because I thought it would be better.

From what you've seen of my classes and my validation, am I doing this "wrong" in such a way that I should change the whole way I'm doing it or is it alright and I just need to find out what is causing my problems and move on?
Was This Post Helpful? 0
  • +
  • -

#27 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

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

Re: First foray into OOP: Correct implementation?

Posted 25 February 2013 - 05:21 PM

View PostDarkranger85, on 25 February 2013 - 05:23 PM, said:

My code was working pretty much as I expected and I decided to go to OOP because I thought it would be better.

every programming paradigm has pro and cons. which one is "better" is relative.


View PostDarkranger85, on 25 February 2013 - 05:23 PM, said:

From what you've seen of my classes and my validation, am I doing this "wrong" in such a way that I should change the whole way I'm doing it or is it alright and I just need to find out what is causing my problems and move on?

I tend to the latter. even I did not master OOP, Iím still learning. though if you want to get better in OOP youíll sooner or later need to look into design patterns.
Was This Post Helpful? 0
  • +
  • -

#28 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 26 February 2013 - 08:03 AM

Alright then, move onward and upward it is.

So I'm back to figuring out the errors array issue.

I'm not the expert by far, but I'm thinking that I should resolve whatever is putting an empty array in my array in the first place rather than building a workaround to circumvent it.

However in all my hours of staring at my validation I can't figure it out.

A little guidance would be appreciated. You can make me work for it, I just hope that I have the no how to figure it out lol.

And what is a good source for learning about design patterns?
Was This Post Helpful? 0
  • +
  • -

#29 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

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

Re: First foray into OOP: Correct implementation?

Posted 26 February 2013 - 08:13 AM

View PostDarkranger85, on 26 February 2013 - 04:03 PM, said:

I'm not the expert by far, but I'm thinking that I should resolve whatever is putting an empty array in my array in the first place rather than building a workaround to circumvent it.

However in all my hours of staring at my validation I can't figure it out.

well, you are the only one with the code.

other than that you can only try to comment out parts of the code and see, what part adds what data.


View PostDarkranger85, on 26 February 2013 - 04:03 PM, said:

And what is a good source for learning about design patterns?

I used google. rather look for blogs than tutorials.
Was This Post Helpful? 0
  • +
  • -

#30 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 26 February 2013 - 08:19 AM

Ok, I'm going to give my code a thorough look over. I'll check the array line by line by line. :)

Wish me luck and tell my family I love them.
Was This Post Helpful? 0
  • +
  • -

  • (8 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »