Welcome Guest, Not a member yet? Register   Sign In
Can you have too many static functions?
#1

(This post was last modified: 06-27-2015, 01:16 AM by jLinux.)

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
Reply
#2

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.
Reply
#3

(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
Reply
#4

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.
Reply
#5

(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.
Reply
#6

(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!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB