征询对CakePHP 3.6弃用TableRegistry::get()静态调用的看法
TableRegistry::get() Static Calls in CakePHP 3.6 I wanted to get everyone's take on CakePHP 3.6 deprecating the static TableRegistry::get() method — personally, I don't think this is a good move at all.
Let me break down why:
LocatorAwareTrait introduces multiple issues
First off, using LocatorAwareTrait has problems on several fronts. Most critically, it violates the Single Responsibility Principle and Separation of Concerns. On top of that, some developers (myself included) avoid traits entirely because we feel they break proper object-oriented design patterns, and we'd much rather use delegation instead.
I actually prefer a delegation approach combined with a flyweight/singleton pattern. I know LocatorAwareTrait wraps delegation logic under the hood, but the issue is it exposes the getTableLocator()/setTableLocator() methods — which can be misused easily.
Take this facade class example:
class Fruits { use \Cake\ORM\Locator\LocatorAwareTrait; public function getApples() { ... } public function getOranges() { ... } // ... other fruit-related methods } $fruits = new Fruits();
I don't want anyone outside the Fruits class to be able to call $fruits->getTableLocator()->get('table') — that's completely outside the scope of what this facade should be responsible for.
Framework adaptability for complex architectures
Another point to consider is framework adaptability. If your app has multiple modules and an architecture that goes beyond a simple layered structure, calling TableRegistry::getTableLocator()->get('table') every time you need access to a model isn't ideal.
A flyweight/singleton class like TableRegistry makes development much more straightforward and convenient by letting you grab the model you need with just a simple get() call.
Honestly, I'd much rather just call TR::get('table') (I've even created my own wrapper class to insulate my app from changes like this, even though it goes against Cake's coding standards).
What does everyone else think about this deprecation?
内容的提问来源于stack exchange,提问作者mbryja

