I am currently playing a little bit with PHP and tried to find a way to convert a class to data I can save in a database ( array / JSON / key value ).
Till that it seems quite easy to use get_obj_vars or cast to array. But later I want to load the data from the database and convert it back to the class I had before so I can continue to work with the data in class format.
Example :
class TestClass {
private $name;
private $number;
private $testClass2;
/* getter + setter */
}
class TestClass2 {
private $name;
private $number;
/* getter + setter */
}
$testclass = new TestClass();
$testclass2 = new TestClass2();
$testclass2->setName("Class2");
$testclass2->setNumber(42);
$testclass->setName("MyName");
$testclass->setNumber(23);
$testclass->setTestClass2($testclass2);
Now I want to convert $testclass to something I can save in the database in a "key-value-table". After that I want to recreate the class with the given data from database.