Jump to content

Access database inside of a validation class


TheFilmGod

Recommended Posts

Hello,

 

For quite some time, I have been using functions to validate form input and access the databse. I have recently started using OOP, which is far faster and better when it comes to programming (performance aside).

 

My question:

How do I access the database within a specific validation class.

 

Example:

 

class validateLogin {

 

    // Class properties

    public $value;

    public $msg;

    public $valid = 0;

 

    // Access database and do some stuff to check

 

}

 

I added in the "public" to show how I would get values into the class from the public variable scope. Would I do the same for the database? A problem would arise because I use a database class to access mysql. Can I have a class within another class? I could use a normal function and basic coding, but I like php classes. Thank you!

 

 

The point of OOP is to have different objects that do 1 job, and use them together. You probably want to create another class to access the database, and have the validate class interact with the database class to insert data (or alternatively, use the database class to validate the info by interacting with the validate class before inserting)

The point of OOP is to have different objects that do 1 job, and use them together. You probably want to create another class to access the database, and have the validate class interact with the database class to insert data (or alternatively, use the database class to validate the info by interacting with the validate class before inserting)

 

Thanks Mike. I have decided to use the validation class to do a basic validation on the input (check if it's not null, length, and valid characters), then I do a basic function check if the values match the database

 

Thank you!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.