我正在尝试使该功能在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);
答案 0 :(得分:1)
我不确定,但是我认为您应该按声明的方式致电。
如果您声明为:
$autoload['libraries'] = array('constant');
您应该这样称呼它:
$totalview=$this->constant->custom_number_format($views);
(小写)。