0

関数内の関数にアクセスする方法について助けがありました。今、関数内から、コンストラクターで開始されているライブラリに到達しようとしています。

私のコードは次のようになります。

<?php
class Test_model extends CI_Model 
{
    private $ci;

    function __construct()
    {
        parent::__construct();

        // Initiate instance
        $ci =& get_instance();

        // Load helper
        $ci->load->helper('text');
    }

    public function check()
    {
        echo $this->uniqueString();
    }

    public function uniqueString($unique = FALSE)
    {
        function random()
        {
            return parent::random_string('alnum',20);
        }

        while($unique === FALSE)
        {
            $random = random();

            $this->db->where('ver_code', $random);
            $this->db->from('user');

            if($this->db->count_all_results() == 0)
            {
                return $random;
                $unique = TRUE;
            }
        }
    }


}
?>

しかし、関数 random からライブラリ ヘルパーにアクセスできません。私が試してみました

parent::random_string('alnum',20);
$ci->random_string('alnum',20);
$this->ci->random_string('alnum',20);
random_string('alnum',20);

次のようなエラーが表示されます。

Call to undefined method Test_model::random_string()
Cannot access self:: when no class scope is active in
Using $this when not in object context in

どのように行うのが正しい方法ですか?

4

0 に答える 0