6

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.

Joris Ooms
  • 11,880
  • 17
  • 67
  • 124
  • 3
    For all your questions about `&` (references):http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Wrikken Jun 07 '11 at 23:48
  • Oh, thanks. I had absolutely no clue what it was called and searching for '&' didn't get me very far ;) I'll make sure to read that! – Joris Ooms Jun 07 '11 at 23:49
  • That's what that topic is indeed for: SO cannot be searched for that kind of thing :) – Wrikken Jun 07 '11 at 23:53

3 Answers3

3

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

Cyclone
  • 17,939
  • 45
  • 124
  • 193
  • Kind of like the & used for pointers in C? Kinda confused. ;( – Joris Ooms Jun 07 '11 at 23:50
  • I'm not very familiar with C, but I'd assume it's similar usage, yes. – Cyclone Jun 07 '11 at 23:51
  • So, just to make sure I understand this right: you declare $foo and pass a string 'foo' to it; then you declare $bar and assign the address (memory location) of $foo to it; then, when you change $bar to 'bar', it actually updates it in the memory location of $foo, and $foo will then return 'bar' because we updated it through $bar refering to &$foo? Correct? – Joris Ooms Jun 07 '11 at 23:55
  • 1
    Yes. And if you echo `$bar` as well, it'll be the same as `$foo`. – Cyclone Jun 07 '11 at 23:58
  • Okay, thank you. I don't yet see how that is useful though, but I guess I'll go read up on it. Thanks a lot! – Joris Ooms Jun 07 '11 at 23:58
  • It's mostly useful in functions. I'll edit my answer to give a function example. – Cyclone Jun 08 '11 at 00:00
3
Community
  • 1
  • 1
Wrikken
  • 69,272
  • 8
  • 97
  • 136
2
  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.

Community
  • 1
  • 1
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • Thank you. I'll make sure to check out the links you provided. I think I understand the basics of the reference thing now; not quite sure why you would use it though.. buuut if it's anything like pointers in C, it makes sense that I don't get it, because I never understood that either ;D – Joris Ooms Jun 07 '11 at 23:57
  • @cabaret: Quick example: I assign `$foo = 1;`, `$bar =& $foo;` and then increment `$foo`. `$bar` is now equal to 2 because it's value references the original `$foo`. – Wesley Murch Jun 08 '11 at 00:00
  • And $foo? Is it still 1 or 2 aswell? Because if it's 2 aswell, why not just use $foo? Probably missing the point here, haha. – Joris Ooms Jun 08 '11 at 00:01
  • @cabaret: Yes, they are both equal to 2. That was just to help you understand references, not a good real-world example. Think of it in the context of the huge CI object, we want to usually reference the original object and pick up on when it changes, not copy the object to a new one in it's current state. – Wesley Murch Jun 08 '11 at 00:03
  • Oh, okay. Well, thanks. I'll go read the documentation! And thanks for getting to this, I've learnt a lot from you since I joined this site :) – Joris Ooms Jun 08 '11 at 00:03
  • FYI none of this is explicitly related to Codeigniter, it's regular PHP. – Wesley Murch Jun 08 '11 at 00:05
  • Yeah, I realise that; just got to it from looking at the CI source, really. – Joris Ooms Jun 08 '11 at 00:06
  • I find that looking at the CI source really demystifies it and makes me want to write my own framework. Yeah right, like I have time for that! I wish. Glad you're reading it and getting a good understanding of how it all really works, it will help you get a lot more out of Codeigniter. – Wesley Murch Jun 08 '11 at 00:08