Objects & References
Posted: Mon Mar 03, 2008 2:33 pm
Hello there.
I am new to PHP; coming from a .NET background.
I am trying to create an object mapping class that can save/load application objects to/from a database.
The theory behind this was to create a "Data_Object" class that knows how to load and save itself to and from the database; this object manages two arrays fields[] and values[], and has two methods __construct() and Save().
If I then extend from this object to create a "Person" object (for example). All I would need to do is define the fields[] of a Person in the constructor and then allow the Parent::__construct() and Parent::Save() to do all the work.
For example, to load an object: I would use an object manager to get a row of data from MySQL that has the fields necessary to build the Person object. I then call "new Person($resultRow)", which calls the Person's constructor which populates the fields[] array, and then passes the $resultRow to the Data_Object constructor "parent::construct($resultData)" to populate the values[] array, i.e. the parent constructor goes through each field[] item and assigns values[fields[$key]] = $resultRow[$key].
Any changes to this array are then monitored (using dirty flags, etc.) and extracted from the values[] array to create an UPDATE SQL statement when the inherited Person->Save() method is called.
Question 1 : What are the problems with this approach?
Secondly, I wanted the ability to update these fields without having to use __set() and __get() to update the arrays. Therefore, in the extended objects, I created public fields (i.e Person.Name) and when the object is being loaded in the parent class I added the lines:
$key = $this->fields[$i];
$this->$key = &$this->values[$key];
The idea behind this referencing was to allow me to write the following code (which can directly update the values[] array):
$pm = new person_manager();
$p = pm.load($person_id);
echo $p->Name;
$p->Name = 'Some New Name';
$p->Save();
Question 2 : Is using public attributes as array references a really bad idea?
Many thanks for reading; please be gentle, I am new to this world.
Regards
P
I am new to PHP; coming from a .NET background.
I am trying to create an object mapping class that can save/load application objects to/from a database.
The theory behind this was to create a "Data_Object" class that knows how to load and save itself to and from the database; this object manages two arrays fields[] and values[], and has two methods __construct() and Save().
If I then extend from this object to create a "Person" object (for example). All I would need to do is define the fields[] of a Person in the constructor and then allow the Parent::__construct() and Parent::Save() to do all the work.
For example, to load an object: I would use an object manager to get a row of data from MySQL that has the fields necessary to build the Person object. I then call "new Person($resultRow)", which calls the Person's constructor which populates the fields[] array, and then passes the $resultRow to the Data_Object constructor "parent::construct($resultData)" to populate the values[] array, i.e. the parent constructor goes through each field[] item and assigns values[fields[$key]] = $resultRow[$key].
Any changes to this array are then monitored (using dirty flags, etc.) and extracted from the values[] array to create an UPDATE SQL statement when the inherited Person->Save() method is called.
Question 1 : What are the problems with this approach?
Secondly, I wanted the ability to update these fields without having to use __set() and __get() to update the arrays. Therefore, in the extended objects, I created public fields (i.e Person.Name) and when the object is being loaded in the parent class I added the lines:
$key = $this->fields[$i];
$this->$key = &$this->values[$key];
The idea behind this referencing was to allow me to write the following code (which can directly update the values[] array):
$pm = new person_manager();
$p = pm.load($person_id);
echo $p->Name;
$p->Name = 'Some New Name';
$p->Save();
Question 2 : Is using public attributes as array references a really bad idea?
Many thanks for reading; please be gentle, I am new to this world.
Regards
P