简体   繁体   中英

Calling php function from inside another function

Maybe you guys can help me I am trying to do this:

public function index()
{
    $r = array();
    //some code   
    echo json_encode($this->utf8ize($r));
}

public function utf8ize($d) {
   //some code
    return $d;
}

But I get the "Call to undefined function utf8ize()" error

Why?

Edit 1: The complete code

<?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 is used to reference the current instance of an object. In your case, you missed the reference to this for the recursive call.

Simple solution - also add $this-> to the inner utf8ize call

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;
}

It looks as if your code is correct.

However I do not see the class defenition nor can I see you using the class.

I created a class on my local machine that works, when I call the method of the test class the function returns an empty array.

<?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();

I hope this helps, if not feel free to reach out :)

Mmm not the best answer but this code works

public function index()
{
    $r = array();
    //some code
    }

    function utf8ize($d) {
        //some code
    }

    echo json_encode(utf8ize($r));
}

I had to put the function inside the original function

Edit: The error was from the recursive call; not the first one.

Thanks to all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM