Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 858
Joris Ooms Avatar asked Jun 07 '11 23:06

Joris Ooms


2 Answers

  • For all your questions about & (references):what-does-this-symbol-mean-in-php(SO)
  • For all your questions about comment blocks look at phpDocumentor
  • For questions about the preceding underscore look at naming conventions
  • The CI_Controller looks like a Singleton
like image 137
Wrikken Avatar answered Sep 22 '22 13:09

Wrikken


& 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.

like image 40
Cyclone Avatar answered Sep 21 '22 13:09

Cyclone