[英]Calling php function from inside another function
也許你們可以幫助我,我正在努力做到這一點:
public function index()
{
$r = array();
//some code
echo json_encode($this->utf8ize($r));
}
public function utf8ize($d) {
//some code
return $d;
}
但是我收到“未定義函數utf8ize()的調用”錯誤
為什么?
編輯1:完整的代碼
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Solicitud extends MX_Controller {
public function __construct()
{
/*
parent::__construct();
if(!$this->input->is_ajax_request())
{
show_404();
exit();
}
else
{
*/
$this->load->model('Solicitud_model', 'Model');
//}
}
public function index()
{
$bandera = $this->input->post('bandera');
$r = array();
if ($bandera == 1){
$result = $this->Model->getConsulta($this->session->session_facultad_apps);
$r = array("data" => $result,
"success" => true,
"bandera" => $bandera);
}else if($bandera == 2)
{
$result = $this->Model->get($this->session->session_facultad_apps);
$r = array("data" => $result,
"success" => true,
"bandera" => $bandera);
}else if ($bandera == 3){
$result = $this->Model->getAsigna($this->session->session_facultad_apps);
$r = array("data" => $result,
"success" => true,
"bandera" => $bandera);
}
echo json_encode(utf8ize($r));
}
public function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = utf8ize($v);
}
} else if (is_string ($d)) {
$d = iconv('UTF-8', 'ISO-8859-1', $d);
return utf8_encode($d);
}
return $d;
}
this
用於引用對象的當前實例。 在您的情況下,您錯過了遞歸調用對此的引用。
簡單的解決方案-還將$this->
添加到內部utf8ize
調用中
echo json_encode($this->utf8ize($r));
...
public function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = $this->utf8ize($v);
}
} else if (is_string ($d)) {
$d = iconv('UTF-8', 'ISO-8859-1', $d);
return utf8_encode($d);
}
return $d;
}
看起來您的代碼是正確的。
但是,我看不到班級划分,也看不到您正在使用班級。
我在可以運行的本地計算機上創建了一個類,當我調用測試類的方法時,該函數將返回一個空數組。
<?php
class Test
{
public function index()
{
$r = array();
echo json_encode($this->utf8ize($r));
}
public function utf8ize($d)
{
//some code
return $d;
}
}
$test = new Test();
echo $test->index();
我希望這會有所幫助,即使不是很忙的時候也可以:)
嗯,不是最好的答案,但是此代碼有效
public function index()
{
$r = array();
//some code
}
function utf8ize($d) {
//some code
}
echo json_encode(utf8ize($r));
}
我不得不將功能放在原始功能內
編輯:錯誤是來自遞歸調用; 不是第一個。
謝謝大家!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.