1

As you know, Codeigniter is a great PHP framework, I'm trying to make my own framework. Here is a problem. I really like the $data functionality in Codeigniter and I want to make it happen in my framework. Question is, how it works. Here is what it does :

  1. you make a array like this :

    $data['title']= 'My Name';
    
  2. then you can use this variable like this in view :

    $title ;
    

How I can make a variable like $data ?

4
  • I like when people try to create their own stuff!. You want to use your data variable in views only, right? Commented Oct 25, 2014 at 7:41
  • 1) "$data functionality" LOL 2) check how CI does it. the whole source code is there... Commented Oct 25, 2014 at 7:42
  • This is not Just for view ... in codeigniter you male the $data['title'] = 'My name'; and then in nest line you use it like this : echo $title; and this is what confused me. Commented Oct 25, 2014 at 7:51
  • 2
    when we pass the variable into the $data array inside a controller, We can use that variable exactly after passing it to $data array in controller at the following lines without doing any thing else and without calling any other modules. NO! Commented Oct 25, 2014 at 12:54

2 Answers 2

5

What you are looking for is turning array keys into variables. There's one in-built function given to do this.

extract() - Import variables into the current symbol table from an array

$data['x'] = "Value";
extract($data,  EXTR_PREFIX_SAME, null);
echo $x;

Go through docs and explore how you want to use it.

Sign up to request clarification or add additional context in comments.

2 Comments

I know this my friend ... Problem is here .... How to make the $data variable do all these stuff itself ... without calling any function directly from us, because this is what CI ( codeigniter ) did it.
@user3492977 CI does this for you. Go through system\coreLoader.php` and look in _ci_load function. You'll see they have used extract. As you are writing framework you need mechanism to load views. While doing that you can use this function as they did or better find another way of loading views. Cheers :)
0

Your question is a bit vague, and also don't write your own framework if you are struggling with this question.

But I think what you are asking is how does CI access the variables in $data (without $data['var_name'];) ?

So if you pass:

$data = array("title" => "a page title");

in your view you can just . Is that your question - how to do that?

it is simple.

function view($data=array(),$file="your-view-file.php") {
extract($data); 
include($file);
}

because extract is in the same scope as the include().

Honestly though, just go through the CI files and work this stuff out...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.