Joris Ooms
Joris Ooms

Reputation: 12038

Some basic questions after looking at the CI sourcecode

I was just looking at the CodeIgniter source code and I came across a couple of things that I can't seem to figure out; I'm not sure what they mean, and since they're mostly like one or two symbols it makes it hard to search on both google and stackoverflow for them.

One thing that I came across quite a lot is this:

$this->config =& get_config();

I have never really encountered the =& (or mostly the &) in PHP before. What does this mean exactly? Are they assigning an instance of get_config to $this->config? I assume the $this->config comes from a declaration at the top of the file where it says var $config = array();

I went looking for the get_config() function, and I found the following line:

function &get_config($replace = array())

Here, my question is pretty much the same: what does the & stand for and what does it do? I see these two things (& and =&) a lot throughout the CI core files.

Something else I was wondering about is their commenting 'style'. Every function starts with a comment block, here's an example:

 /**
 * Set HTTP Status Header
 *
 * @access  public
 * @param   int     the status code
 * @param   string
 * @return  void
 */

Is this generated by some plugin or library? It sounds like a lot of hassle to do this manually. I haven't checked out stuff like PHPDoc, but could this be something similar (or PHPDoc)? It seems useful, if it generates that automatically? Heehee.

Onto the next question. I see different functions prefixed by underscores. There's the obvious __construct but there's also functions like _set_default_controller(); and _set_routing(); Do these underscores have any special meaning? I know the double underscore is used for something called 'magic methods' (I'm thinking about __get and __set since those are the ones I've used myself). Do they have any 'special' technical meaning or is this pure semantics? Enlighten me if possible.

Last but not least, in the controller core file I saw this:

class CI_Controller {

    private static $instance;

    public function __construct()
    {
        self::$instance =& $this;
                // goes on

The line of interest here is self::$instance =& $this; What does this mean? Does it set $thisto an instance of itself (wiiiiiild guess, haha), so we can use $this? Or does that make no sense? Actually it doesn't, since in the very basic MVC boilerplate I use myself for basic websites, I use $this without any of that advanced stuff.

Can anyone offer some insight here? I'd be grateful. Thanks a lot in advance.

Upvotes: 6

Views: 168

Answers (3)

Cyclone
Cyclone

Reputation: 18295

& is for passing something by reference, meaning any changes you make to the variable that you assigned it to will affect the original variable. It essentially sends it the memory location instead of the value.

Here's the php.net documentation for references.

Example:

$foo = 'foo';
$bar = &$foo;
$bar = 'bar';
echo($foo);
//Should output "bar"

Why can this be useful?

function everythingButFirst($s){
    return(substr($s,1));
}

function everythingButFirstV2(&$s){
    $s = substr($s,1);
}

//First example: Without reference
$str = "abcde";
$str = everythingButFirst($str);
//Will set $str to bcde

//Second example: With reference
$str = "abcde";
everythingButFirstV2($str);
//Will set $str to bdce

It saves a bit of typing with assignment, you see. Much easier to call a function than call a function and assign it to a variable.

Upvotes: 3

No Results Found
No Results Found

Reputation: 102745

  1. The & operator there is assigning a value by reference, meaning further use of this variable will reference the original value, not the assigned one. Reference (no pun intended): http://php.net/manual/en/language.references.php

  2. The comments are phpdoc style, they aren't generated themselves, but can be handy in creating docs with phpdoc or other software, and picking up expected params and return values in an IDE.

  3. The underscore usually means the method is private. When used in a CI controller, it means the method is inaccessible by url. Related: What's the deal with a leading underscore in PHP class methods?

  4. You are pretty much correct. The function get_instance() will return the $instance property of Controller.

Upvotes: 2

Wrikken
Wrikken

Reputation: 70460

Upvotes: 3

Related Questions