Objects & References

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
PWhite
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2008 1:52 pm

Objects & References

Post by PWhite »

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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Objects & References

Post by Christopher »

All sounds good ... not sure there is a question there. It looks like either RowDataGateway or ActiveRecord. The only thing you will need to be aware of is that ActiveRecord can't currently be done with a static like some other languages do it. That adds a line of code, but is no different otherwise. Do you have some code to show? Or do you want some examples?
(#10850)
PWhite
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2008 1:52 pm

Re: Objects & References

Post by PWhite »

Brilliant. Thanks for your speedy reply and caveats.

I think the best thing for me to do is go off and have a good read up on RowDataGateway, etc.

Thanks again.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Objects & References

Post by Mordred »

You shouldn't need 'fields' and 'values', see get_class_vars() and get_object_vars().
Post Reply