I'm after a model-based database class for PHP/MySQL. But it must have one particular functionality:
Once the class is initialised ( e.g $user=new User(); ) I should be able to access the attributes in the user table as follows:
$user->name // returns the value of the 'name' field in the user table
I should not have to define the attributes or need to create any functions to return the attribute value, it should be done automatically when the class is initialised.
Is there such a database class out there? If not can someone tell me how I would go about creating this functionality?
I am unsure on a database class, but if you were to write one then look into PHP's Magic Methods, in particular the __get() function
http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members
class myClass {
function __get($field) {
// grab $field from database
}
function __set($field, value) {
// set $field in database
}
}
$class = new myClass();
echo $class->field1; // grab value field1
$class->field1 = 'myVal'; // set field1 = 'myVal';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With