# |
May 26th 2017, 11:04 |
hmic |
thats the cake way you have asked for |
# |
May 26th 2017, 11:03 |
hmic |
with a registry! |
# |
May 26th 2017, 11:03 |
obinoob |
hmic: let me expose my ideia with a small code example I think it can clear up things a bit, this is a stupid argument probably but I just want to find a way I can reuse a custom object instance in my code ;) |
# |
May 26th 2017, 11:01 |
hmic |
you could easily utilize the generic objectregistry to load and get your custom class when-/whereever |
# |
May 26th 2017, 11:01 |
hmic |
there are more registries uses in cake than the tableregistry |
# |
May 26th 2017, 11:00 |
hmic |
if thats not what you want, you can use a registry to get the instance whenever and whereever you need it. exactly like the table class you get from the tableregistry |
# |
May 26th 2017, 10:59 |
hmic |
why every method? if the trait is attached to a class, you can add the instance to that class |
# |
May 26th 2017, 10:59 |
obinoob |
available for trait and all the classes that use trait's codes |
# |
May 26th 2017, 10:59 |
hmic |
but still: i'd add the functionality to te table, from what you have said. |
# |
May 26th 2017, 10:58 |
obinoob |
I just want to have one instance |
# |
May 26th 2017, 10:58 |
obinoob |
but I've found I need a new instance in every method on the trait |
# |
May 26th 2017, 10:58 |
hmic |
use a registry! |
# |
May 26th 2017, 10:58 |
hmic |
and you need a way to access it |
# |
May 26th 2017, 10:58 |
obinoob |
the custom class instance is being used in trait |
# |
May 26th 2017, 10:58 |
hmic |
so* |
# |
May 26th 2017, 10:57 |
hmic |
you have been talking about a trait |
# |
May 26th 2017, 10:57 |
obinoob |
I've a trait and a custom class |
# |
May 26th 2017, 10:57 |
obinoob |
hmic: I know that |
# |
May 26th 2017, 10:57 |
hmic |
a trait is no class |
# |
May 26th 2017, 10:57 |
hmic |
or use a dependency injection container (which cake does not in the core IMHO) but there exist plugins for cake that provide you with that functionality too |
# |
May 26th 2017, 10:57 |
obinoob |
hmic: I'm using the TableRegistry at my custom class |
# |
May 26th 2017, 10:56 |
hmic |
so in your case, you eighter want to utilize this registry pattern - use ones that cake already has |
# |
May 26th 2017, 10:56 |
obinoob |
hmic: either by a static method TableRegistry or by calling $this->table |
# |
May 26th 2017, 10:56 |
hmic |
through the TableRegistry! this is the most commonly used/exposed registry that cake provides. but there are more. they are globally accessible and provide you with object instances |
# |
May 26th 2017, 10:55 |
hmic |
how do you access a table object? |
# |
May 26th 2017, 10:55 |
hmic |
think of the tables classes again please |
# |
May 26th 2017, 10:54 |
obinoob |
and I could call it on the class that I will be using the trait |
# |
May 26th 2017, 10:54 |
hmic |
thats not what you want |
# |
May 26th 2017, 10:54 |
hmic |
still you need to access the same object instance from there, so you need to pass it in |
# |
May 26th 2017, 10:54 |
obinoob |
hmic: but I can have a constructor on a trait |
# |
May 26th 2017, 10:54 |
hmic |
additionally, you dont want to pass the object instance around likely |
# |
May 26th 2017, 10:53 |
hmic |
only the object the trait is attached to can have state. |
# |
May 26th 2017, 10:53 |
hmic |
first off: a trait has no state. |
# |
May 26th 2017, 10:53 |
obinoob |
hmic: ok I get the ideia and I agree with it, but lets pretend i need to create some custom class it can be a component it doesn't matter for the example |
# |
May 26th 2017, 10:50 |
hmic |
this can be added from a trait, if you think thats a good idea. i dont. it could be a behavior attached to your table too, which sounds way better |
# |
May 26th 2017, 10:49 |
hmic |
wait, the user database queries should be custom finders *in your table* |
# |
May 26th 2017, 10:49 |
obinoob |
hmic: *sorry a single UserData instance |
# |
May 26th 2017, 10:48 |
obinoob |
hmic: I've a trait that I am using for app business rules in controller but I've found that i could make my life much simpler if i could wrap all user database queries in a class so now I need a way of create a single UserData class to use in my trait |
# |
May 26th 2017, 10:46 |
hmic |
and the usecase |
# |
May 26th 2017, 10:46 |
hmic |
obinoob, maybe you explain your goal first |
# |
May 26th 2017, 10:45 |
obinoob |
I don't want to create a new object instance in every method, I'm sure there must be a way ;) a cakes way |