Some basic questions after viewing the CI source code

I was just looking at the source code of CodeIgniter, and I came across a couple of things that I cannot understand; I'm not sure what they mean, and since they are mostly similar to one or two characters, this makes it difficult for them to search both google and stackoverflow.

One thing that I have encountered quite a bit is this:

$this->config =& get_config(); 

I have never come across =& (or basically & ) in PHP before. What does it mean? Are they an instance assignment from get_config to $this->config ? I assume $this->config comes from the declaration at the top of the file where var $config = array(); indicated var $config = array();

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

 function &get_config($replace = array()) 

Here my question is about the same: what does & mean and what does it do? I see these two things ( & and =& ) in all the main CI files.

Something else that I was curious about was their "style" of comments. Each function starts with a comment block, here is 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 it manually. I have not tested things like PHPDoc, but could it be something similar (or PHPDoc)? Seems useful if it automatically generates this? Heehee.

To the next question. I see different functions with an underscore prefix. There is an obvious __construct , but there are also functions like _set_default_controller(); and _set_routing(); . Do these underscores have any particular meaning? I know that double underscores are used for something called "magic methods" (I think of __get and __set , since these are the ones I used myself). Do they have a β€œspecial” technical meaning or is this pure semantics? Enlighten me if possible.

And 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 is here self::$instance =& $this; What does it mean? Did he install $this instance of himself (wiiiiiild guess, haha) so we can use $ this? Or does it make no sense? Actually, this is not so, since in the most basic MVC template I use myself for basic sites, I use $ this without any advanced things.

Can someone give you some idea? I would be thankful. Thank you very much in advance.

+6
source share
3 answers
  • The & operator assigns a value by reference, which means that further use of this variable will refer to the original value, and not to the assigned one. Link (no pun intended): http://php.net/manual/en/language.references.php

  • Comments are phpdoc style, they are not generated by themselves, but can be useful when creating documents with phpdoc or other software, as well as collect expected parameters and return values ​​in the IDE.

  • Underscore usually means private . When used in a CI controller, this means that the method is not reachable by URL. Related: What's the matter with leading underscore in PHP class methods?

  • You are pretty much true. The get_instance() function will return the $instance Controller property.

+2
source

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

Here's the php.net documentation for links.

Example:

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

Why might this be helpful?

 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 

As you can see, this saves a bit of input with the task. It is much easier to call a function than to call a function and assign it to a variable.

+3
source
+3
source

Source: https://habr.com/ru/post/890006/


All Articles