Jump to content

Dealing with large forms - OOP


NiTx

Recommended Posts

Hey Guys,

 

I've just started to learn OOP. So far I'm lovin' it, but I want to get some advice. I'm dealing with a form that has around 100 fields and I'm trying to find the most efficient way to work my variables.

 

Here's a small example.

class Review extends DatabaseObject {
	
	protected static $table_name = "reviews";

	public static $required_fields = array(
		'security',
		'location',
		'training',
		'comments'
		
	);
	
	public static $nonRequired_fields = array(
		'pay',
		'pay_rate',
		'tips'
	);

        public static $generated_fields = array(
		'company_id',
		'id',
		'user_id',
		'date_added'
	);
        


protected function attributes() {
		global $company;
		global $user;
		
		$attributes = array();
		foreach(self::$required_fields as $field) {
			if(property_exists($this, $field)) {
				$attributes[$field] = $this->$field;
			}
		}
		
		foreach(self::$nonRequired_fields as $field) {
			if(property_exists($this, $field)) {
				$attributes[$field] = $this->$field;
			}
		}
		if(isset($_SESSION['user_id'])) {
			$attributes['user_id'] = $_SESSION['user_id'];
		}
		$attributes['company_id'] = $company->company_id;
		$attributes['total_score'] = $this->total_score;

		return $attributes;
	}

As you can see I've put these variables into arrays to make form validation easier, but when it comes to instantiating my class, I run into some issues. I know this is most likely a stupid approach.

I apologize because I know I haven't really explained what I'm after. I have to be as efficient as possible since I'm dealing with such a large number of form fields and I'm not sure which way is the best approach with OOP.

 

I hope my code is enough for you to get a good enough idea.

 

Please help and thanks :)

Edited by NiTx
Link to comment
Share on other sites

My first question: Why does this class extend a DatabaseObject class yet you keep talking about form validation? What is this class actually meant to represent?

 

All classes that are associated to a table in my database Inherit this class as it contains my Create, Update and Delete functions. So as to save me rewriting that code multiple times in each class.

 

My question is why are you using global variables? That to me defeats using OOP a little bit.

 

Sorry, I'm still very new to OOP so I wouldn't know why. Seems still viable to me, and to the person who's tutorial I've been watching. I'm all ears though, the tutorials I've been watching are aged and I don't want to start any bad habits early.

 

100 form fields? The only one's who are going to fill that in is you (for testing) and web crawling bots (to ruin your life). 100 form fields is no different in OOP then 5 form fields.

 

Your're right. It is only for me but I plan to have admins that could make mistakes. It's really hard to explain what I'm asking you guys without posting a ton of code. I should have spent more time structuring my post as to explain myself better.

 

To put it into perspective.. Lets say you had a database table with 110 columns and a form with 100 fields. You have 60 fields which are required fields, 40 fields that are not required. The remaining 10 variables are generated fields like id, admin_id and date_added etc.. 

 

All these different variables have to go through a different process in my code before getting submitted to the database. So have I done the right thing by grouping my variables into arrays so that each group of variables can be processed in the appropriate way?

Edited by NiTx
Link to comment
Share on other sites

 

 

All classes that are associated to a table in my database Inherit this class as it contains my Create, Update and Delete functions. So as to save me rewriting that code multiple times in each class.

 

Then your class depends on your DatabaseObject class, it doesn't need to extend it. Pass it in to your class and use it from there.

 

 

Sorry, I'm still very new to OOP so I wouldn't know why. Seems still viable to me, and to the person who's tutorial I've been watching. I'm all ears though, the tutorials I've been watching are aged and I don't want to start any bad habits early.

 

Classes are meant to encapsulate data. Pulling data in from the global scope breaks that encapsulation.

Link to comment
Share on other sites

Then your class depends on your DatabaseObject class, it doesn't need to extend it. Pass it in to your class and use it from there.

 

 

 

Sorry, I'm still very new to OOP so I wouldn't know why. Seems still viable to me, and to the person who's tutorial I've been watching. I'm all ears though, the tutorials I've been watching are aged and I don't want to start any bad habits early.

 

Classes are meant to encapsulate data. Pulling data in from the global scope breaks that encapsulation.

 

 

Thanks trq, I see what you mean. I'm trying to get myself familiar with OOP concepts but i'm failing to do so! :S Is pulling in from the global scope a complete no-no? Take my basic find_by_sql function as an example.. Do you not have a database class to make database queries? And if you do, how do you make queries without pulling it in form the global scope?

	public static function find_by_sql($sql="") {
		global $database;
		$result_set = $database->query($sql);
		$object_array = array();
		while ($row = $database->fetch_array($result_set)) {
			$object_array[] = self::instantiate($row);
		}
		return $object_array;
	}
Link to comment
Share on other sites

how do you make queries without pulling it in form the global scope?

You inject it into your object on instantiation.

 

class DatabaseObject
{}

class SomeClassWithDBDependency
{
  protected $db;

  public function __construct($db) {
  {
    $this->db = $db;
  }

  public function foo()
  {
    return $this->db->query();
  }   
}

$obj = new SomeClassWithDBDependency(new DatabaseObject);
This code is still missing a few important OOP principals, but it should explain what I meen by my last comment.
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.