In article <fn****************@newssvr27.news.prodigy.com>, Zurab Davitiani wrote:
I finally got around to trying this solution in PHP5, and it worked OK - one
thing missing is a static $instance property definition inside the class. I
still don't get what is the point of this though - maybe developers who
don't read docs - enforce factory class programatically?
Simple example:
Say you've got:
<?php
class Invoice() { .. }
class Item() { ... }
// You want to do:
$item = $invoice->getItem("P100");
echo $item->title();
echo $item->getInvoice()->title(); // get the invoice title.
?>
The idea here is that at no point can anything except Invoice() generate
an instance of Item. (Item has a private constructor)
Furthermore. Say Item is an interface or abstract class, the actual items
are different, but all of them implement an "Item" interface. This way,
you could have a Clothing Item, Electronics Item, etc.. each with
methods specific to the item type. You also can be reasonably assured
that all Item()'s haven't been constructed outside an Invoice().
You know when working with the code that each Item must have a parent
Invoice instance, down the road, if an Item required access to stuff in
the Invoice class (such as a is_sales_tax() method ?) You would be able
to access it, w/out giving access to other areas of the application. (So
that if is_sales_tax() were to change, (lets say sales tax became
dependant on country or something) you would know it was accessed ONLY
in Invoice() and any Item's it generated, making changes easier)
Aside from this, PHP5 is looking really _really_ good. :-)
Jamie
--
http://www.geniegate.com Custom web programming
User Management Solutions Perl / PHP / Java / UNIX