Hitesh Chauhan
Hitesh Chauhan

Reputation: 1074

How to make the function Global in Codeigniter application

I am trying to make the function global in CodeIgniter application. I have created a PHP file Constant.php in application/libraries.

Constant.php

<?php 

 defined('BASEPATH') OR exit('No direct script access allowed');

 class Constant
 {

public function custom_number_format($n, $precision = 1) {
if ($n < 900) {
    // 0 - 900
    $n_format = number_format($n, $precision);
    $suffix = '';
} else if ($n < 900000) {
    // 0.9k-850k
    $n_format = number_format($n / 1000, $precision);
    $suffix = 'K';
} else if ($n < 900000000) {
    // 0.9m-850m
    $n_format = number_format($n / 1000000, $precision);
    $suffix = 'M';
} else if ($n < 900000000000) {
    // 0.9b-850b
    $n_format = number_format($n / 1000000000, $precision);
    $suffix = 'B';
} else {
    // 0.9t+
    $n_format = number_format($n / 1000000000000, $precision);
    $suffix = 'T';
}
 // Remove unecessary zeroes after decimal. "1.0" -> "1"; "1.00" -> "1"
 // Intentionally does not affect partials, eg "1.50" -> "1.50"
if ( $precision > 0 ) {
    $dotzero = '.' . str_repeat( '0', $precision );
    $n_format = str_replace( $dotzero, '', $n_format );
}
return $n_format . $suffix;
 }

 }

I have declared this library in the config/autoload.php something like

$autoload['libraries'] = array('constant');

and now I trying to access this library something like

$totalview=$this->Constant->custom_number_format($views);

But I am getting the following error enter image description here

Upvotes: 0

Views: 37

Answers (1)

Silvio Delgado
Silvio Delgado

Reputation: 6975

I'm not totally sure, but I think you should call as declared.

If you declared it as:

$autoload['libraries'] = array('constant');

You should call it as:

$totalview=$this->constant->custom_number_format($views);

(with lowercase).

Upvotes: 1

Related Questions