Welcome Guest, Not a member yet? Register   Sign In
Help with classes
#1

[eluser]eckelarsson[/eluser]
Hello!
Need help with a class question.

See my code here below:
Code:
class model_search extends Model {
    
    public static $my_static = $this->uri->segment(4);
    
    function model_search()
    {
        parent::Model();
    }
    
    
    function fetch_count_resultat($fritext)
    {
        // SÖKKRITERER
        $filter_postort = $this->uri->segment(4);
        $SNI = $this->uri->segment(5);
        
        if($filter_postort == 'allt') { } else { $this->db->where('Postort', $filter_postort); }
        if($SNI == 'SNI') { } else { $this->db->where('SNI_kod', $SNI); }
        
        $this->db->like('Foretag', $fritext);
        $this->db->or_like('Postort', $fritext);
        
        // ---->
        $this->db->from('register');
        
        echo self::$my_static;
        return $this->db->count_all_results();
        
        
    }
}

What i want is to have a few static constants that i can aply with every function of this class. It works if the static look like this;
Code:
public static $my_static = 'foo';
Not if i write:
Code:
public static $my_static = $this->uri->segment(4);

Dose this make some sense?
#2

[eluser]xwero[/eluser]
you can't use the class variable definition to fetch dynamic data.
Code:
// OK
public  $var1 = 1;
public  $var2 = true;
public  $var3 = 'true';
public  $var4 = array();
#3

[eluser]Evil Wizard[/eluser]
Something like this is what you are looking for but then the "static" class variable is not static at all
Code:
class model_search extends Model {
    
    public static $my_static = NULL;
    
    function __construct()
    {
        parent::__construct();
        self::$my_static = $this->uri->segment(4);
    }
    function fetch_count_resultat($fritext)
    {
        // SÖKKRITERER
        $SNI = $this->uri->segment(5);
        
        if(self::$my_static !== 'allt') { $this->db->where('Postort', self::$my_static); }
        if($SNI == 'SNI') { } else { $this->db->where('SNI_kod', $SNI); }
        
        $this->db->like('Foretag', $fritext);
        $this->db->or_like('Postort', $fritext);
        
        // ---->
        $this->db->from('register');
        
        echo self::$my_static;
        return $this->db->count_all_results();
    }
}
Why not just assign the segment to a normal protected class variable?
#4

[eluser]eckelarsson[/eluser]
[quote author="Evil Wizard" date="1243518028"]Something like this is what you are looking for but then the "static" class variable is not static at all
Code:
class model_search extends Model {
    
    public static $my_static = NULL;
    
    function __construct()
    {
        parent::__construct();
        self::$my_static = $this->uri->segment(4);
    }
    function fetch_count_resultat($fritext)
    {
        // SÖKKRITERER
        $SNI = $this->uri->segment(5);
        
        if(self::$my_static !== 'allt') { $this->db->where('Postort', self::$my_static); }
        if($SNI == 'SNI') { } else { $this->db->where('SNI_kod', $SNI); }
        
        $this->db->like('Foretag', $fritext);
        $this->db->or_like('Postort', $fritext);
        
        // ---->
        $this->db->from('register');
        
        echo self::$my_static;
        return $this->db->count_all_results();
    }
}
Why not just assign the segment to a normal protected class variable?[/quote]

This worked great! Thank you so much
#5

[eluser]Evil Wizard[/eluser]
Although that's not technically what static class variables are meant for.
Code:
class model_search extends Model {
    
    protected $my_var;
    
    function __construct()
    {
        parent::__construct();
        $this->my_var = $this->uri->segment(4);
    }
    function fetch_count_resultat($fritext)
    {
        // SÖKKRITERER
        $SNI = $this->uri->segment(5);
        
        if($this->my_var !== 'allt') { $this->db->where('Postort', $this->my_var); }
        if($SNI == 'SNI') { } else { $this->db->where('SNI_kod', $SNI); }
        
        $this->db->like('Foretag', $fritext);
        $this->db->or_like('Postort', $fritext);
        
        // ---->
        $this->db->from('register');
        
        echo self::$my_static;
        return $this->db->count_all_results();
    }
}
is a much more elegant way to utilise class properties, IMHO




Theme © iAndrew 2016 - Forum software by © MyBB