views:

36

answers:

4

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?

Thanks.

+1  A: 

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.

John Conde
+3  A: 

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.

middaparka
A: 

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.

echo
A: 

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 :)

Ninjastyle