Welcome Guest, Not a member yet? Register   Sign In
Dynamically loading a class and calling a function.
#1

[eluser]steel_slasher[/eluser]
I am working on a CMS and I have made a parser for an array which contains details on where to get data for a template. The library which parses the array will load a model or library and then call a function which is required to retrieve the information.

The array for the parser:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['main'] =     array(
                        array(
                            'block'=>'text',
                            'type' => 'static',
                            'title' => 'First piece of text',
                            'content' => 'Second piece of text'
                            ),
                        array(
                            'block'=>'text',
                            'type' => 'static',
                            'title' => 'Third piece of text',
                            'content' => 'Fourth piece of text'
                            ),
                        array(
                            'block'=>'text',
                            'type' => 'static',
                            'title' => 'Fifth piece of text',
                            'content' => 'Sixth piece of text'
                            ),
                        array(
                            'fauxvar' => 'news',
                            'file'=>'news',
                            'type' => 'dynamic',
                            'class' => 'model',
                            'title' => 'headlines()',
                            'content' => 'frontpage()'
                            )
                        )

?>

The actual parser itself:
Code:
function content($controller)
    {
        $this->CI =&get;_instance();
        $mainobject = '$this';
        $data = array();
        $config = $this->CI->load->controller_config($controller);
        if(is_array($config['main']))
        {
            log_message('Debug', 'Controller config is array');
            for($i=0;$i<count($config['main']);$i++)
            {
                if($config['main'][$i]['type'] == 'static')
                {
                    $data[$config['main'][$i]['block']][] = $config['main'][$i];
                }
                
                if($config['main'][$i]['type'] == 'dynamic')
                {
                    if($config['main'][$i]['class'] == 'model')
                    {
                        $this->CI->load->model($config['main'][$i]['file']);
                        log_message('DEBUG','Model loaded, from array!');
                    }
                    
                    if(isset($config['main'][$i]['title']))
                    {
                        $class = strtolower($config['main'][$i]['file']);
                        call_user_func(array(&$this->CI->{$class},$config['main'][$i]['title']));
            //Alternate doesn't work either
                        $this->CI->{$class}->{$config['main'][$i]['title']};
                    }

/*Ignore                    
            if(isset($config['main'][$i]['content']))
                    {
                        $titles = call_user_func(array(&$this->CI->$config['main'][$i]['file'], $config['main'][$i]['title']));
                    }*/
                }
            }
        }
        return $data;
    }
#2

[eluser]steel_slasher[/eluser]
can noone really help me
#3

[eluser]TheFuzzy0ne[/eluser]
Do you get some kind of error?
#4

[eluser]steel_slasher[/eluser]
These are the two errors i get:
Code:
A PHP Error was encountered

Severity: Warning

Message: call_user_func(News::headlines()) [function.call-user-func]: First argument is expected to be a valid callback

Filename: libraries/Theme.php

Line Number: 77
A PHP Error was encountered

Severity: Notice

Message: Undefined property: News::$headlines()

Filename: libraries/Theme.php

Line Number: 78

Lines they refer to are;

Code:
call_user_func(array(&$this->CI->{$class},$config['main'][$i]['title']));
$this->CI->{$class}->{$config['main'][$i]['title']};
#5

[eluser]pistolPete[/eluser]
It seems as if the callback function doesn't exist:
Code:
$config['main'][$i]['title']
#6

[eluser]TheFuzzy0ne[/eluser]
Your title have spaces in them, needless to say you can't have a callback function containing spaces, yet that appears to be how you're calling it.




Theme © iAndrew 2016 - Forum software by © MyBB