开发者

Can I require class-functions only if the class is instantiated in php?

Trying to improve my code's agility and loading time, I thought of doing this, so that the functions would be required开发者_如何转开发 only if the class was instantiated.

class User
{
    public function __construct()
    {
       require('all_user_fns.php');
    }
}

Inside all_user_fns.php will reside all User class's functions.

Is that feasible (maybe in another way)? Does it have any advantages when it comes to speed?


To be honest this will be slower (you're having to do an additional include after all) although meaninglessly so. As such, if speed is really an issue look at one of the opcode caches such as the Alternative PHP Cache (APC)

Somewhat more concerning, it's going to make maintaining the code painful and will quite possibly cause issues when attempting to use visibility keywords such as private/protected/public depending on what IDE you're using.


If those functions exist just to modify and interact with user objects they they should be member functions of that class. That's the point of objects, to encapsulate and organize related functionality.


My guess: Won't have any noticeable impact on speed. Plus it makes your code harder to understand and maintain. I'd steer clear and look for other ways to speed things up.


I'd suggest moving the user functions to the User class itself as methods - it makes more sense and is more easily maintained. If you think maintenance is not an issue, let me know in 6 months and tell me how did that work you for you :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜