Welcome Guest, Not a member yet? Register   Sign In
call static method from model
#1

[eluser]Unknown[/eluser]
if i had understand the userguide if i want use a methode from model i should
code like this.

Code:
$this->load->model('Commercial', '', TRUE);
        $data['commande'] = $this->Commande->SelectCommande();        
        $data['commercial'] = $this->Commercial->SelectCommercial();

but if i want use a static method in order to not instanciate the object (that is the goal
of a static methoc) ?

apparently the load method use a singleton, it's better than anything, but if i want no instancitiation because of memory cost for example, should i write my controller like this:


Code:
require_once(myclass.php);
$data = myclass::mymethod(parameter);

By the way before write this post i have tried to set the static attribut, and it generate a warning. Normal ?

it quite ugly no ? any answer or solution ?
Thanks
#2

[eluser]Sumon[/eluser]
A bunch of code might be helpful for you. Whether i don't know using static variable are good practice or not. Anyway, in my project i use this static variable in some classes inside \application\libraries\

Here is an example class and how to use function.
Code:
class Custom_config extends Controller
{
    function Custom_config()
    {
        //parent::Controller();    //Strictly Prohibited.
    }

    function MemberProfileInfo($MemberId)
    {
        global $Data;
        $query = $this->db->query("SELECT P.*, L.* FROM member_profile P, member_login L WHERE L.mem_id=P.member_id AND P.member_id='$MemberId'");
        if ($query->num_rows() > 0)
        {
            $row = $query->row();
            $Data['MemberFirstName']=$row->mem_first_name;
            $Data['MemberLastName']=$row->mem_last_name;
            $Data['MemberEmail']=$row->mem_email;
        }
    }

}
Now i in my config\autoload.php i load this class. And call this function frequently from many controllers as
Code:
// Inside method of a class
static $Data;
Custom_config::MemberProfileInfo($MemberId);
echo $Data['MemberEmail']; // i get member email instantly.

however, i am not sure this is a good practice or not. So i also like to know about it.
#3

[eluser]Unknown[/eluser]
Thanks for your proposition, i was wondering if i would not use a librairy to use static method too.

I 'm not sure that ur obliged to imply inheritance from controller if u don't use parent::Controller ... Wink
i will try your proposition, and compare with others

thx
#4

[eluser]Rick Jolly[/eluser]
If you want to use a static method, either don't use the CI loader, or make a static class a helper. Unlike Models and Libraries, the CI loader doesn't instantiate helpers - because it doesn't expect them to be classes. All it does is include them. There is nothing stopping you from putting a class with static methods in the helpers directory though.




Theme © iAndrew 2016 - Forum software by © MyBB