On Thursday, June 27, 2002, at 04:44 AM, Andrew White wrote:
> On Wednesday, June 26, 2002, at 04:48 pm, Erik Price wrote:
>> My problem is that I have no way to know which type of object to
>> instantiate in advance -- all I have is this primary key. Using the
>> primary key to instantiate an object, I can extract the ProjectType
>> primary key, and then I could instantiate a new object of the
>> appropriate type, but then I'd have two objects (and that seems like a
>> lot of overhead). I'd rather just have the client code instantiate
>> the right type of object from the beginning.
> Use a static function in the project parent class and a column in the
> table which stores the classname of the project
I did not realize that you could access a class method as a static
method in this fashion! (Class::method()) Enlightening. So I can use
Project class code without even having any instantiated Project
objects. That is really cool.
However, this is really the same thing as if I did the same thing from
the client code. In other words, it does require an extra database call
to determine the type of Project, to help me decide which kind of
project to instantiate. This way it just puts that code into the class
definition to keep my client code from getting cluttered.
I do not believe it is possible to cast an object from one user-defined
data type to another in PHP -- this is what my own testing has shown,
though I have not heard confirmation. This would really be the ultimate
solution. But still, the above is a good workaround. My alternative is
to simply use the Project class and -not- subclass the different types
of projects, and then use a big ugly Project::display() method that does
its own testing of the type of the object and performs certain code
depending. That would save me the extra database call at least, so for
performance it might be worthwhile.
Thanks very much Andrew.
Web Developer Temp
Media Lab, H.H. Brown
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php