3

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?

4
  • 1
    What you are looking for is called an ORM (Object relational mapping) solution. Commented Jul 6, 2010 at 15:42
  • Cheers. This might be what I'm looking for: phpactiverecord.org Commented Jul 6, 2010 at 15:58
  • Only problem is that particular ORM needs PHP 5.3. Anyone know of any other solutions? Commented Jul 6, 2010 at 17:09
  • I doubt this is possible with PHP < 5.3 if you do not want to do at least some configuration. For full automagic you will need an ActiveRecord base class to inherit from that utilizes Late Static Binding. Commented Jul 6, 2010 at 17:45

4 Answers 4

2

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';
Sign up to request clarification or add additional context in comments.

1 Comment

This looks good. But I assume I would still need to manually define the field names? I want complete automation, so for example if I were to add a new field to the database I should just be able to access it without adding any new code. I suppose what's needed is for the class needs to go in to the table, get the list of field names and add them as properties of the object.
2

Check out http://www.phpobjectgenerator.com/. Will same you a lot of time in the long run.

Comments

1

Have you looked at Doctrine? You can generate model classes based on your DB schema. I use it on every project and I love it.

2 Comments

I had a look at Doctrine but I can't make any sense of it. Have you got any usage examples for finder and crud functions? Also did you get version 1.2 stable or 2.0 beta?
Anybody else used Doctrine? How easy is it to use?
0

You can use stdClass to do just that:

$object = new StdClass;  
$object->foo = 'bar'; 

Or if you want a little bit more control you could use PHP's magic methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.