Jump to content

Input stays in form after submit


php-beginner

Recommended Posts

This does also work but am not sure if this is allowed:

 

I change the message class in my user class to public;

I change the function where I "echo" to "return $this->errorMessages;"

 

Now I add this in my registration script to check if the object has been set ... :

 

<?php if(isset($user)){ 

if($user->message->messageStatus != false){

	echo 'Oops...';
	echo '<ul>';
		foreach($user->error as $msg){
		echo '<li>'. $msg .'</li>';
		}
	echo '</ul>';
}

}

?>

 

This means that I can remove this code from my user class because this is already set on the registration script:

 

<?php

if($this->message->messageStatus != false){
		// etc;
	}
?>

 

This way I don't have to rewrite lots of code.

Link to comment
Share on other sites

Instead of making $message public, keep it private and make a new method that returns the result of $message->messageStatus.  Something like:

 

public function isValid()
{
   return $this->message->messageStatus();
}

 

So, you'd use

 

if (!$user->isValid())
{
   // error code
}

 

Much more concise, and it leaves your encapsulation intact.

Link to comment
Share on other sites

Great,

 

Thankyou very much for all your help.

 

I have only one question left:

 

Is the way I make use of object orientated programming good? Or did I wrong interpreted the way objects should be handled?

 

You would probably do it differently, but everyone has his own way I think.

 

Anyway, I'm new to OOP and I hope I understand it quite well now.

Link to comment
Share on other sites

You're at the beginning stages of learning OOP.  You have an idea of the syntax, although you're still missing some pieces there (static methods in particular), and I'm sensing you don't really understand, or are even aware of, the ideas behind OOP in general.  There's a lot more to OOP than simply wrapping things in a class and calling it a day.  If you want to do OOP for real, then at the very least you should look at the following two books:

 

PHP 5: Objects, Patterns, and Practice by Matt Zandstra - http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/dp/143022925X/ref=sr_1_1?ie=UTF8&qid=1304177284&sr=8-1

Design Patterns: Elements of Reusable Object-Oriented Software by the Gang of Four - http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?s=books&ie=UTF8&qid=1304177303&sr=1-1

 

The second book, in particular, will open your eyes to some things.

Link to comment
Share on other sites

Okay,

 

Could you be more specific of what I don't understand. What I know is that objects should be re-usable, thats what I'm doing. And if ever needed in other projects, I could simply use the classes that are already made and leave the classes I don't need.

 

Thanks for your suggestions, I'll take a look at those books.

Link to comment
Share on other sites

Okay,

 

Could you be more specific of what I don't understand. What I know is that objects should be re-usable, thats what I'm doing. And if ever needed in other projects, I could simply use the classes that are already made and leave the classes I don't need.

 

Thanks for your suggestions, I'll take a look at those books.

 

There's more to objects than just making things that are reusable.  Reusable code can be, and is, written in a procedural manner, too.  A lot of the 'magic' of OOP actually lies in being able to change behaviors at run time.

 

From what little I've seen, you're at an intermediary point.  You know that modularity is good, and that objects can be composed (that is, an object can contain another object), but that's about it.  You still need to learn about abstraction and polymorphism.  You also need to learn some of the common patterns of object creation and interaction.  That's where the two books, especially the second, come into play.

Link to comment
Share on other sites

Do you mean by "change behaviors at run time" that I shouldn't call my objects (like message, encryption, formValidator etc.) in the user class, instead call them on the registration script?

 

So that all the objects doesn't form as one system in the user class, but as one system at run time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.