Welcome Guest, Not a member yet? Register   Sign In
How can I use custom class?
#1

[eluser]greedyman[/eluser]
I have a class as below, I putted it in helpers folder and autoload it:
Code:
<?php
    class text {
        /**
         * @instance object
         */
        private static $instance;
        
        public function __construct() { }
        
         public static function get_instance() {
             if (empty(self::$instance)) {
                 self::$instance = new text();
             }
             return self::$instance;
         }


         public function remove_sign($str_input) {
            $sign = array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ",
                           "ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ","ì","í","ị","ỉ","ĩ",
                           "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ",
                           "ờ","ớ","ợ","ở","ỡ",
                           "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
                           "ỳ","ý","ỵ","ỷ","ỹ",
                           "đ",
                           "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă",
                           "Ằ","Ắ","Ặ","Ẳ","Ẵ",
                           "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
                           "Ì","Í","Ị","Ỉ","Ĩ",
                           "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ",
                           "Ờ","Ớ","Ợ","Ở","Ỡ",
                           "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
                           "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
                           "Đ","ê","ù","à");
            $no_sign = array("a","a","a","a","a","a","a","a","a","a","a",
                             "a","a","a","a","a","a",
                             "e","e","e","e","e","e","e","e","e","e","e",
                             "i","i","i","i","i",
                             "o","o","o","o","o","o","o","o","o","o","o","o",
                             "o","o","o","o","o",
                             "u","u","u","u","u","u","u","u","u","u","u",
                             "y","y","y","y","y",
                             "d",
                             "A","A","A","A","A","A","A","A","A","A","A","A",
                             "A","A","A","A","A",
                             "E","E","E","E","E","E","E","E","E","E","E",
                             "I","I","I","I","I",
                             "O","O","O","O","O","O","O","O","O","O","O","O",
                             "O","O","O","O","O",
                             "U","U","U","U","U","U","U","U","U","U","U",
                             "Y","Y","Y","Y","Y",
                             "D","e","u","a");
             return str_replace($sign, $no_sign, $str_input);
        }
    }
?>

And use it inside a view file:
Code:
$obj = $this->text->get_instance();
echo $obj->remove_sign('Tôi yêu VN');
But i got an error, have anyone know what is it?
Quote:A PHP Error was encountered

Severity: Notice
Message: Undefined property: CI_Loader::$text
Filename: user/welcome.php
Line Number: 10
$obj = $this->text->get_instance();

Fatal error: Call to a member function get_instance() on a non-object in C:\xampp\htdocs\dpfoods\application\views\user\welcome.php on line 10
#2

[eluser]Otemu[/eluser]
Hi,
Quote:Helpers are not written in an Object Oriented format. They are simple, procedural functions.

Here an example taken from StackOverflow that should help you:

Create a file and put the following code into it.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('test_method'))
{
    function test_method($var = '')
    {
        return $var;
    }  
}

Save this to application/helpers/ . We shall call it "new_helper.php"

The first line exists to make sure the file cannot be included and ran from outside the CodeIgniter scope. Everything after this is self explanatory.
Using the Helper

This can be in your controller, model or view (not preferable)

Code:
$this->load->helper('new_helper');

echo test_method('Hello World');

If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file.

Code:
$autoload['helper'] = array('new_helper');

#3

[eluser]greedyman[/eluser]
[quote author="Otemu" date="1361804003"]Hi,
Quote:Helpers are not written in an Object Oriented format. They are simple, procedural functions.

Here an example taken from StackOverflow that should help you:

Create a file and put the following code into it.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('test_method'))
{
    function test_method($var = '')
    {
        return $var;
    }  
}

Save this to application/helpers/ . We shall call it "new_helper.php"

The first line exists to make sure the file cannot be included and ran from outside the CodeIgniter scope. Everything after this is self explanatory.
Using the Helper

This can be in your controller, model or view (not preferable)

Code:
$this->load->helper('new_helper');

echo test_method('Hello World');

If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file.

Code:
$autoload['helper'] = array('new_helper');

[/quote]

Thanks in advanced! I'll try to search more instead of give questions.
#4

[eluser]CroNiX[/eluser]
You can also use your own classes. See the userguide for Creating your own Libraries.
#5

[eluser]greedyman[/eluser]
[quote author="CroNiX" date="1361837770"]You can also use your own classes. See the userguide for Creating your own Libraries.[/quote]

Thanks! I done.




Theme © iAndrew 2016 - Forum software by © MyBB