CodeIgniter Forums

Full Version: Can you have too many static functions?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ive been working with CI a lot recently, and I setup my models so the methods could be used in a static context... Example below:
Code:
<?php

class Accounts_model extends CI_Model
{
   private static $db;

   public function __construct()
   {
       parent::__construct();

       $CI =& get_instance();

       self::$db = $CI->db;

   }

   public static function get_id_from_username($username = NULL)
   {
       self::$db->select('Accounts.account_id')
                ->where('Accounts.username', $username)
                ->from('Accounts');

       $query = self::$db->get();

       return $query->result();
   }
}

And with that kinda setup... since when you load a model in CI, the class is only loaded once, I can create every method as static...

My question, is can you have TOO MANY static methods? is it bad practice? LMK whatcha think. I mean it works fine, since I dont load the classes more than once.. so idk
It is unknown practice, it is hard to say.
As far as I know, loading a model with a different property name in the controller instance makes a new instance of the model.
(06-27-2015, 12:52 AM)ivantcholakov Wrote: [ -> ]It is unknown practice, it is hard to say.
As far as I know, loading a model with a different property name in the controller instance makes a new instance of the model.

Right, but if the model is configured to auto load, then its just loaded once, thus its no big deal?... Just didnt want to fill my entire project up with static references, then find out it was a bad idea
A lot of people will tell you that static methods are bad no matter what - simply because they're more challenging to TEST. While it can be a little bit tricky, and difficult to maintain state if you're doing any HMVC modules::run() code, I find that there are times when it is a great thing to use and makes sense. Most of the time, I would stick to the non-static method, though.
(06-27-2015, 01:16 AM)jLinux Wrote: [ -> ]
(06-27-2015, 12:52 AM)ivantcholakov Wrote: [ -> ]It is unknown practice, it is hard to say.
As far as I know, loading a model with a different property name in the controller instance makes a new instance of the model.

Right, but if the model is configured to auto load, then its just loaded once, thus its no big deal?... Just didnt want to fill my entire project up with static references, then find out it was a bad idea

If you're only loading the model once, why are you using static properties/methods? Your static method doesn't work without the constructor, and the static $db property duplicates the inherited $this->db.

Generally, the purpose of static properties/methods in a class are to allow multiple instances of a class to share some data, or to make the methods available without having to instantiate the class.

Since CI loads everything into a singleton, the use cases for static properties/methods are diminished, as you usually have only one instance of each class, and most of those classes are accessible from any other loaded class through the CI instance.
(06-29-2015, 07:23 AM)mwhitney Wrote: [ -> ]
(06-27-2015, 01:16 AM)jLinux Wrote: [ -> ]
(06-27-2015, 12:52 AM)ivantcholakov Wrote: [ -> ]It is unknown practice, it is hard to say.
As far as I know, loading a model with a different property name in the controller instance makes a new instance of the model.

Right, but if the model is configured to auto load, then its just loaded once, thus its no big deal?... Just didnt want to fill my entire project up with static references, then find out it was a bad idea

If you're only loading the model once, why are you using static properties/methods? Your static method doesn't work without the constructor, and the static $db property duplicates the inherited $this->db.

Generally, the purpose of static properties/methods in a class are to allow multiple instances of a class to share some data, or to make the methods available without having to instantiate the class.

Since CI loads everything into a singleton, the use cases for static properties/methods are diminished, as you usually have only one instance of each class, and most of those classes are accessible from any other loaded class through the CI instance.

Your very last sentence..

Quote:Since CI loads everything into a singleton, the use cases for static properties/methods are diminished, as you usually have only one instance of each class, and most of those classes are accessible from any other loaded class through the CI instance.
Is something I realized shortly after this thread, so yeah, reverted my changes. THANKS!