Welcome Guest, Not a member yet? Register   Sign In
Static Class inside library folder
#1

[eluser]Nicholas Hanks[/eluser]
I tried to create static class with all static methods inside library folder however I was not able to do that because by default Codeigniter tries to instantiate the class inside the library folder which my class design does not allow. My code is like this.
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class Data
{
    private static $_reader;
    private static $_instance = null;
    private static $_articles = null;

    private function  __construct()
    {
        $path = substr(BASEPATH, 0, -8).'\xmls\data.xml';
        try
        {
            self::$_reader = simplexml_load_file($path);            
        }
        catch (Exception $e)
        {
            echo $e->getMessage();
        }
    }

    public static function get_article($id)
    {
        self::_getInstance();
        $articles = self::get_articles();
        $result = array();

        foreach($articles as $article)
        {
            if($article['id'] == $id)
            {
                $result['id'] = $article['id'];
                $result['title'] = $article['title'];
                $result['heading'] =  $article['heading'];
                $result['content'] = $article['content'];
                break;
            }
        }
        return $result;
    }

    public static function article_exist($id)
    {
        $result = false;
        $id = trim($id);

        if(preg_match( '/^[0-9]+$/', $id))
        {
            $articles = self::get_articles();

            foreach($articles as $article)
            {
                if($article['id'] == $id)
                {
                    $result = true;
                    break;
                }
            }
        }
        return $result;
    }

    public static function get_articles()
    {
        self::_getInstance();
        if(!is_array(self::$_articles))
        {
            self::$_articles = array();
            foreach(self::$_reader->article as $article)
            {
                self::$_articles[] = array
                (
                    'id' => (int)$article['id'],
                    'title' => (string)$article->title,
                    'heading' =>  (string)$article->heading,
                    'content' => (string)$article->content
                );
            }
        }
        return self::$_articles;
    }

    private static function _getInstance()
    {
        if(!isset( self::$instance ))
        {
            $class = __CLASS__;
            self::$_instance = new $class();
        }
        return self::$_instance;
    }

}
/*
* End of class Data
* End of file helper/data_helper.php
*/
Now currently if I put this file inside helper folder then it works but logically speaking this class does not belongs inside helper folder. In controller I would like to call this class in this way.
Code:
if(Data::article_exist($id))
{
   $data['article'] = Data::get_article($id);
}
What is the proper way to do this kind of stuff in CI.


Messages In This Thread
Static Class inside library folder - by El Forum - 05-25-2010, 08:08 AM
Static Class inside library folder - by El Forum - 05-25-2010, 08:25 AM
Static Class inside library folder - by El Forum - 05-25-2010, 08:25 AM
Static Class inside library folder - by El Forum - 05-25-2010, 02:40 PM
Static Class inside library folder - by El Forum - 05-25-2010, 04:13 PM
Static Class inside library folder - by El Forum - 05-26-2010, 09:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB