Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How Does Instance Function Work?
  • So I was curious as to why instance('test1') is not calling factory but is instead just returning false if that particular instance doesn't already exist. I thought that instance worked as follows: It looks to see if an object named "test1" already exists and if it does it gets returned. If it doesn't exist then factory('test1') is called. Is this not how Fuel works?
  • To add a little bit to what WanWizard already said: We're using 2 patterns here, mixed in with some bits of a third: 1. Factory Pattern
    2. Multiton Pattern
    3. Singleton Pattern *light* The factory pattern is used to create NEW objects, especially usefull if the class uses drivers and might return different types of classes. The multiton pattern keeps references to all instances that are created and allows you to fetch them by name, IF they exist. Otherwise it'll return false. Also parts of the Singleton pattern are still present as well in some cases because some classes have a default instance (configured by a config file and created upon request) that is available for convencience and allows you to use just Class::instance() without a param.
  • So say I have a method action_signup() and in it I say $fieldset = Fieldset::factory('signup_user');
    If I visit http://mysite.com/index/whatever/signup twice it does not seem to throw an error. I guess my question becomes, when does an object in fuel get destroyed? Or was the first Fieldset named 'signup_user' not deleted?
  • Objects are destroyed either when you destroy them, or when the PHP script ends.
  • instance() returns existing instance by name, factory() creates new instances. The main reason why instance() doesn't auto-create instances is that in most cases, factory() requires extra parameters and/or configuration to be able to create the desired instance. You don't pass these when you call instance(). It also provides a very clean implementation of the factory pattern, and an easy to understand API: factories create, instances are fetched. It would be confusing if instance() for one class would auto-create the instance, and not for another class.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion