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 $this
to 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.
&
(references):what-does-this-symbol-mean-in-php(SO)&
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With