Model database class required (PHP / MySQL)

I am following a database class for PHP / MySQL. But it must have one specific functionality:

After initializing the class (for example, $ user = new User ();) I must have access to the attributes in the user table as follows:

$ user-> name // returns the value of the name field in the user table

I do not need to define attributes or create any functions to return the attribute value, this should be done automatically when the class is initialized.

Is there such a database class? If not, will someone tell me how I will create this functionality?

+3
source share
4 answers

, , PHP Magic Methods, __get()

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';
+2

Doctrine? . , .

+1

stdClass, :

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

, , PHP .

0

Source: https://habr.com/ru/post/1753335/


All Articles