如何在Codeigniter应用程序中将功能设置为全局

时间:2019-04-13 04:32:34

标签: php codeigniter codeigniter-3

我正在尝试使该功能在CodeIgniter应用程序中具有全局性。我在Constant.php中创建了一个PHP文件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;
 }

 }

我已经在config/autoload.php中声明了这个库,例如

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

现在我试图访问该库,例如

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

但是出现以下错误 enter image description here

1 个答案:

答案 0 :(得分:1)

我不确定,但是我认为您应该按声明的方式致电。

如果您声明为:

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

您应该这样称呼它:

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

(小写)。