I have a State class that includes configuration as well as some state-specific methods. I also have a Factory class that I inhertit my MVC classes from that also has a reference to this State object. This makes sure that all of my MVC objects have specific State-related functionality. For example, my State class includes methods for setting and reading cache or sessions.
My Factory class has a number of methods that are simply wrappers, like this:
public function myFunction($data){
$this->State->myFunction($data);
}
The idea is that these methods are defined in Factory, but are actually executed by State object. This allows me to call:
$this->myFunction(); rather than the longer one that includes State in it, when writing code.
I don’t know enough about how PHP works internally, but is this efficient? My thought process has been that since I usually only have one instance of State object, then having method wrappers in my Factory class is better, because otherwise those methods would include a lot of code that is not used most of the time. But I am not sure how PHP works internally.
At times I can have dozen objects that have the parent Factory class and all of them reference the same State object. Is it efficient to have wrapper methods defined in Factory that simply call methods of State, rather than deal with State directly? Or am I just adding additional overhead to the whole system?
My idea was that if Factory is simply a class that is inherited and includes wrappers for a lot of functionality, then I can streamline development more efficiently. Does it save memory if the class that you inherit from has wrappers as opposed to fully defined code in methods?
No, such wrapper has not performance benefit. Actually, what you get this was is an extremely minor loss of performance.
What’s more important, this way you are hiding dependencies, which might make it harder to maintain the code.
Few thoughts on your topic
I get the impression, that you are doing it all wrong tm. Here are the issues, that I have with what you wrote in the question:
Is the
Stateclass responsible for configuration OR “state specific methods”?There is such thing as SRP, which states, that every class should have a single responsibility. You could also say, that every class should have only one season to change.
Why do your “MVC classes” inherit from
Factoryclass?In object oriented code, the keyword
extendsrepresents an is-a relationship between classes. It’s OK to haveclass Oak extends Tree, but when you writeclass User extends Table, it is complete insanity .. and kinda insulting. This is covered by LSP (for a simplified explanation: look here).You have misunderstood the purpose of
Factorypattern.Factory is a creational pattern. This means, that the purpose of factories is to return object instances. Instead of inheriting some methods, which provide access to
Stateinstance, you should inject it in each of created structure.This will have the added benefit that all the structures, which
Factoryinstance created, share the sameStateinstance.Seems like you are trying to have global state thought-out your codebase
If every in your application need access to instance of
State, then something is deeply wrong with the whole architecture. You end up with situation where a single object exists in all layers, thus causing a major leak in abstractions. You basically are just hiding a global variable.