简体   繁体   English

Yii和静态函数

[英]Yii and static functions

Is it bad practice to have a lot of static functions? 拥有很多静态函数是不好的做法吗? I am using Yii framework and I realized I have a lot of static functions in my model classes. 我正在使用Yii框架,我意识到我的模型类中有很多静态函数。 I put all my functions that have to do with Users into the UsersModel (I do the same for other models too) but I end up with a lot of static functions. 我把与User有关的所有函数都放到了UsersModel中(我也对其他模型做了同样的事情),但我最终得到了很多静态函数。 Just wondering how you guys deal with this. 只是想知道你们如何处理这个问题。 A lot of these functions are just query builder functions instead of lazy loading because I need to increase database performance. 很多这些函数只是查询构建器函数而不是延迟加载,因为我需要提高数据库性能。

Example functions: 示例功能:

User::getUserFromCampaign(1)
User::getUsersNotInCamapaigns()
User::isAdmin()

Instead of using static functions, what you can do is instantiate an object of the User class (which would presumably not be a static class) in the models that require those methods, and use the object's methods directly. 您可以做的不是使用静态函数,而是在需要这些方法的模型中实例化User类的对象(可能不是静态类),并直接使用对象的方法。

This also means that those methods will only be loaded on objects that require them, instead of being "global". 这也意味着这些方法只会加载到需要它们的对象上,而不是“全局”。

Here is a good answer on this : https://softwareengineering.stackexchange.com/questions/98083/cant-i-just-use-all-static-methods 以下是一个很好的答案: https//softwareengineering.stackexchange.com/questions/98083/cant-i-just-use-all-static-methods

Instantiating your classes is best for testing but there is no problem using static for certain tasks. 实例化您的类最适合测试,但对于某些任务使用静态没有问题。

A lot of it is down to opinion, if your code works, it's running efficiently and easy to maintain then all is dandy! 很多都是观点,如果您的代码有效,它运行高效且易于维护,那么一切都很花哨!

Also to add to the Laravel facade comments. 还要添加到Laravel门面评论。 Laravel does instantiate the class, a facade just provides a simplified interface to the bigger picture lets say.. that's exactly what laravel is doing. Laravel确实实例化了这个类,一个外观只是为更大的图片提供了一个简化的界面,让我们说..这正是laravel正在做的事情。 The end result is really nice readable code. 最终结果是非常好的可读代码。

MVC is a nice design pattern and it has its place. MVC是一个很好的设计模式,它有它的位置。 The Factory design pattern is another nice pattern. 工厂设计模式是另一个很好的模式。 In case you're unfamiliar with it, Google for Factory design pattern . 如果您不熟悉它,Google for Factory设计模式 In a few words: a FooFactory is a class that generates objects of the Foo class (or FooModel class, if you will). 简而言之:FooFactory是一个生成Foo类对象的类(如果愿意,还可以生成FooModel类)。

MVC and Factory are not mutually exclusive, so you could refactor a lot of those static methods into a new UserFactoryClass. MVC和Factory不是互斥的,因此您可以将许多静态方法重构为新的UserFactoryClass。

  • User::getUserFromCampaign(1) to me, is a bit weird, though. User::getUserFromCampaign(1)对我来说, User::getUserFromCampaign(1) I take it that 1 is a campaign ID? 我认为1是广告系列ID? Then what user does it return? 然后用户返回什么? Or can a campaign only have one user? 或者广告系列只能有一个用户? If that is the case, then UserFactory::getUserFromCampaign() will return a UserModel object for the user in the campaign with the given ID. 如果是这种情况,则UserFactory::getUserFromCampaign()将为具有给定ID的广告系列中的用户返回UserModel对象。

  • User::getUsersNotInCampaign() , I assume, returns an array of UserModel objects? 我假设User::getUsersNotInCampaign()返回一个UserModel对象数组? Refactor it into `UserFactory::getUsersNotInCampaign() and there you go. 将它重构为`UserFactory::getUsersNotInCampaign()然后你就去了。

  • User::isAdmin() shouldn't be static at all. User::isAdmin()根本不应该是静态的。 if ($user->isAdmin()) ... , not if(User::isAdmin($user))... if ($user->isAdmin()) ... ,而不是if(User::isAdmin($user))...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM