LemonInflux Posted April 12, 2008 Share Posted April 12, 2008 Hello. I've been looking through the gallery and phpBB source code, to learn how it is constructed, and I've come across 2 things I haven't met before: & and _ For example, in the gallery core, they have 2 functions, function &getStorage() and _shutdown(). What is the significance of & and _? I can't find a simple, understandable explanation Thanks in advance, Tom Link to comment https://forums.phpfreaks.com/topic/100764-and-_-a-human-explanation/ Share on other sites More sharing options...
Gamic Posted April 12, 2008 Share Posted April 12, 2008 The "&" symbol is usually used when passing values to a method by reference (or passing values from a method by reference), rather than by value. Have a look at the manual to find out more about passing by reference. The "__" is usually used to denote an automatic method. For example "__construct()" will be called when a new object is created from a class. <?php //some example code class foo{ private $message; public function __construct($pMessage="Hello World"){ $this->setMessage($pMessage); } public function getMessage(){ return $this->message; } public function setMessage($pVal){ $this->message=$pVal; } } $br="<br />\n"; $test=new foo();//message will be "Hello World" $bar=new foo("Goodbye World");//message will be "Goodbye World" echo $test->getMessage().$br.$bar->getMessage().$br; $bar=&$test;//$bar now points to the same object as $test. $test->setMessage("The Corn!"); echo $bar->getMessage();//should outuput "The Corn!" ?> Output: C:\xampp\php>php example.php Hello World<br /> Goodbye World<br /> The Corn! C:\xampp\php> Link to comment https://forums.phpfreaks.com/topic/100764-and-_-a-human-explanation/#findComment-515384 Share on other sites More sharing options...
LemonInflux Posted April 12, 2008 Author Share Posted April 12, 2008 I understand now! Thanks! So if I had, in my class: function _say($text="blah") { echo $text; } then if I said $foo = new foo(); (if the class is called foo), it would echo "blah" unless I had defined $text as something else, regardless of whether I call the function or not? Link to comment https://forums.phpfreaks.com/topic/100764-and-_-a-human-explanation/#findComment-515388 Share on other sites More sharing options...
Gamic Posted April 12, 2008 Share Posted April 12, 2008 There is a list of automatic functions, or magic methods. The two common ones are __construct and __desctruct. in your case it would echo blah if you called the _say function, and only if you called the _say function as _say is not an automatic function that php will recognize as a magic method. Have a read of this entry in the manual to learn more about magic methods. Link to comment https://forums.phpfreaks.com/topic/100764-and-_-a-human-explanation/#findComment-515397 Share on other sites More sharing options...
Daniel0 Posted April 12, 2008 Share Posted April 12, 2008 The "__" is usually used to denote an automatic method. For example "__construct()" will be called when a new object is created from a class. What LemonInflux asked about was a method called _shutdown() (1 underscore). When you're prepending class properties or methods with a single underscore it's signifying that the member is to be considered private or protected. This is a convention used for pre-PHP5 scripts as versions before that did not support member visibility. The ones with two underscores are called magic methods. Link to comment https://forums.phpfreaks.com/topic/100764-and-_-a-human-explanation/#findComment-515406 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.