Intro
The BeanFactory
is a great way to load and query for beans without
using SQL.
For example we can load an Account with a known id:
$bean = BeanFactory::getBean('Accounts', '1234');
Unexpected behaviour
However this method can cause some unexpected behaviour. Let’s look at the following example: wzxhzdk:1
This produces the following output:
Acc1 Name: Bar
Acc2 Name: Bar
Acc1 Name: Bar
Acc2 Name: Bar
Acc3 Name: Foo
Enter the cache
So what is going on here?
Basically the getBean
method is cached. getBean
will cache at most
10 beans and drop some of the older entries based on usage. $acc2
is
pulled from the cache and is therefore the same instance as $acc1
. Any
changes in one will be reflected in the other.
However when we load a bunch of beans our cached account drops out the
BeanFactory Cache and when we fetch the bean into $acc3
we get a fresh
instance from the database.