[FLOW3-general] Object Framework vs. new

Robert Lemke robert at typo3.org
Mon Feb 22 11:18:04 CET 2010


Hi folks,

as a general rule of thumb for those not developing the FLOW3 core itself but your very own packages:

 - Use "new" only in your unit tests, or when instantiating Exceptions.
 - Use Dependency Injection whenever possible for retrieving singletons.
 - Use create() for instantiating prototypes

If you use "new" instead of FLOW3's functions,

 - you won't get dependencies injected
 - you won't get automatically built AOP proxy classes
 - FLOW3 cannot manage the singleton or session scope of that object
 - the security framework doesn't know / care about your object
 - your object cannot be persisted

For the Reflection Service the case is a bit different because it is part of the FLOW3 core. With hen and egg problem we are referring to the challenge that the Reflection Service must be available even before the Object Framework is initialized. But these special cases really only occur in the FLOW3 package.

One remark at last: In alpha 8 you'll see a heavily refactored Object Framework which comes which a big speed gain. And while working on it, I took the (probably last) chance to slightly clean up the API again: There won't be an Object Factory in the future (but it'll stay there for two versions, for backwards compatibility). Instead you'll only have the Object Manager which provides these two important methods:

  create($objectName, $argument1, $argument2, ...)
  get($objectName)
 
Note that create() moved from the Object Factory to the Object Manager and that getObject() is now simply called get(). 

But don't worry about that yet - with the release of alpha 8, I'll post further information, including a little script for migrating (most of) your existing code.

Cheers,
robert

Am 19.02.2010 um 14:22 schrieb Michael Sauter:

> On 19.02.10 08:57, Martin Kutschker wrote:
>> Michael Sauter schrieb:
>>> 
>>> Or is the ONLY reason to use new for the reflection package because it
>>> is used very early in the bootstrap?
>> 
>> That's what the message on forge says, isn't it?
> 
> No, it doesn't.
> 
> 2 reasons are mentioned:
> 1) The classes in the reflection subpackage are not subject to AOP or Persistence, nor are there any singletons.
> 2) The hen-egg problem
> 
> I don't understand #1 and would greatly appreciate any clarification ... plus, I'm a bit confused cause Robert explicitly said that #2 isn't the case ;)
> 
> Thanks,
> Michael
> 
> _______________________________________________
> FLOW3-general mailing list
> FLOW3-general at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/flow3-general



More information about the FLOW3-general mailing list